ex3_error/
internal.rs

1use std::fmt;
2
3use derive_more::Display;
4use thiserror::Error;
5
6use crate::{
7    def_error_base_on_kind, impl_error_conversion_with_adaptor, impl_error_conversion_with_kind,
8};
9
10/// An error with no reason.
11#[derive(Error, Debug, Clone, Copy)]
12#[error("no reason is provided")]
13pub struct SilentError;
14
15/// An error with only a string as the reason.
16#[derive(Error, Debug, Clone)]
17#[error("{0}")]
18pub struct OtherError(String);
19
20#[derive(Debug, PartialEq, Eq, Clone, Copy, Display)]
21pub enum InternalErrorKind {
22    /// Persistent data had corrupted
23    DataCorrupted,
24
25    /// Error occurs during database operations
26    Database,
27
28    /// Unknown system error
29    System,
30
31    /// The feature is disabled or is conflicted with the configuration
32    Config,
33
34    /// Canister access error
35    Canister,
36
37    /// Other system error
38    Other,
39}
40
41def_error_base_on_kind!(InternalError, InternalErrorKind, "Internal error.");
42impl_error_conversion_with_kind!(InternalError, crate::ErrorKind::Internal, crate::Error);
43
44impl_error_conversion_with_kind!(OtherError, InternalErrorKind::Other, InternalError);
45impl_error_conversion_with_adaptor!(OtherError, InternalError, crate::Error);
46
47impl OtherError {
48    /// Creates an error with only a string as the reason.
49    pub fn new<T>(reason: T) -> Self
50    where
51        T: fmt::Display,
52    {
53        Self(reason.to_string())
54    }
55}