Skip to main content

ic_testkit/pic/
errors.rs

1use candid::Principal;
2
3use super::{PicSerialGuardError, startup::PicStartError};
4
5///
6/// PicCallError
7///
8
9#[derive(Clone, Debug, Eq, PartialEq)]
10pub struct PicCallError {
11    pub message: String,
12}
13
14///
15/// PicInstallError
16///
17
18#[derive(Debug, Eq, PartialEq)]
19pub struct PicInstallError {
20    canister_id: Principal,
21    message: String,
22}
23
24///
25/// StandaloneCanisterFixtureError
26///
27
28#[derive(Debug)]
29pub enum StandaloneCanisterFixtureError {
30    SerialGuard(PicSerialGuardError),
31    Start(PicStartError),
32    Install(PicInstallError),
33}
34
35impl PicCallError {
36    /// Capture one PocketIC call/codec failure.
37    #[must_use]
38    pub fn new(message: impl Into<String>) -> Self {
39        Self {
40            message: message.into(),
41        }
42    }
43}
44
45impl std::fmt::Display for PicCallError {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        f.write_str(&self.message)
48    }
49}
50
51impl std::error::Error for PicCallError {}
52
53impl PicInstallError {
54    /// Capture one install failure for a specific canister id.
55    #[must_use]
56    pub const fn new(canister_id: Principal, message: String) -> Self {
57        Self {
58            canister_id,
59            message,
60        }
61    }
62
63    /// Read the canister id that failed to install.
64    #[must_use]
65    pub const fn canister_id(&self) -> Principal {
66        self.canister_id
67    }
68
69    /// Read the captured panic message from the install attempt.
70    #[must_use]
71    pub fn message(&self) -> &str {
72        &self.message
73    }
74}
75
76impl std::fmt::Display for PicInstallError {
77    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78        write!(
79            f,
80            "failed to install canister {}: {}",
81            self.canister_id, self.message
82        )
83    }
84}
85
86impl std::error::Error for PicInstallError {}
87
88impl std::fmt::Display for StandaloneCanisterFixtureError {
89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90        match self {
91            Self::SerialGuard(err) => write!(f, "{err}"),
92            Self::Start(err) => write!(f, "{err}"),
93            Self::Install(err) => write!(f, "{err}"),
94        }
95    }
96}
97
98impl std::error::Error for StandaloneCanisterFixtureError {
99    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
100        match self {
101            Self::SerialGuard(err) => Some(err),
102            Self::Start(err) => Some(err),
103            Self::Install(err) => Some(err),
104        }
105    }
106}