Skip to main content

canic_testkit/pic/
errors.rs

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