canic_core/
lib.rs

1//! CANIC crate utilities for multi-canister apps on the Internet Computer.
2pub mod auth;
3pub mod config;
4pub mod env;
5pub mod guard;
6pub mod ids;
7pub mod interface;
8pub mod log;
9pub mod macros;
10pub mod model;
11pub mod ops;
12pub mod spec;
13
14pub use ::canic_cdk as cdk;
15pub use ::canic_macros::{impl_storable_bounded, impl_storable_unbounded, perf, perf_start};
16pub use ::canic_memory::{eager_init, eager_static, ic_memory, ic_memory_range};
17pub use ::canic_types as types;
18pub use ::canic_utils as utils;
19
20pub mod export {
21    pub use ::ctor;
22}
23
24pub use thiserror::Error as ThisError;
25
26use crate::cdk::{
27    call::{CallFailed, CandidDecodeFailed, Error as CallError},
28    candid::{CandidType, Error as CandidError},
29};
30use serde::Deserialize;
31
32///
33/// Crate Version
34///
35
36pub const CRATE_NAME: &str = env!("CARGO_PKG_NAME");
37pub const VERSION: &str = env!("CARGO_PKG_VERSION");
38
39///
40/// Error
41///
42/// top level error should handle all sub-errors, but not expose the child candid types
43///
44
45#[derive(CandidType, Debug, Deserialize, ThisError)]
46pub enum Error {
47    #[error("{0}")]
48    AuthError(String),
49
50    #[error("{0}")]
51    ConfigError(String),
52
53    #[error("{0}")]
54    CustomError(String),
55
56    #[error("{0}")]
57    EnvError(String),
58
59    #[error("{0}")]
60    InterfaceError(String),
61
62    #[error("{0}")]
63    ModelError(String),
64
65    #[error("{0}")]
66    OpsError(String),
67
68    #[error("{0}")]
69    SerializeError(String),
70
71    ///
72    /// Test Error
73    /// as we don't want to import dev-dependencies
74    ///
75
76    #[error("{0}")]
77    TestError(String),
78
79    ///
80    /// Common IC errors
81    ///
82    /// CallError          : should be automatic with ?
83    /// CallFailed         : use this for wrapping <T, String> return values
84    /// CandidError        : for decode_one errors etc.  automatic
85    /// CandidDecodeFailed : automatic for calls like ::candid<T>()
86    ///
87
88    #[error("call error: {0}")]
89    CallError(String),
90
91    #[error("call failed: {0}")]
92    CallFailed(String),
93
94    #[error("candid error: {0}")]
95    CandidError(String),
96
97    #[error("candid decode failed: {0}")]
98    CandidDecodeFailed(String),
99}
100
101macro_rules! from_to_string {
102    ($from:ty, $variant:ident) => {
103        impl From<$from> for Error {
104            fn from(e: $from) -> Self {
105                Error::$variant(e.to_string())
106            }
107        }
108    };
109}
110
111impl Error {
112    /// Build a custom error from a string without defining a new variant.
113    #[must_use]
114    pub fn custom<S: Into<String>>(s: S) -> Self {
115        Self::CustomError(s.into())
116    }
117
118    /// Build a test error to avoid extra dev-only dependencies.
119    #[must_use]
120    pub fn test<S: Into<String>>(s: S) -> Self {
121        Self::TestError(s.into())
122    }
123}
124
125from_to_string!(auth::AuthError, AuthError);
126from_to_string!(config::ConfigError, ConfigError);
127from_to_string!(env::EnvError, EnvError);
128from_to_string!(interface::InterfaceError, InterfaceError);
129from_to_string!(model::ModelError, ModelError);
130from_to_string!(ops::OpsError, OpsError);
131
132from_to_string!(CallError, CallError);
133from_to_string!(CallFailed, CallFailed);
134from_to_string!(CandidDecodeFailed, CandidDecodeFailed);
135from_to_string!(CandidError, CandidError);