ex3_canister_error/
common.rs1use derive_more::Display;
2use ex3_common_error_info::ErrorInfo;
3use std::fmt;
4use thiserror::Error;
5
6use crate::{def_error_base_on_kind, ErrorCode, ErrorKind, SECOND_LEVEL_ERROR_CODE_INTERVAL};
7
8#[derive(Error, Debug, Clone, Copy)]
10#[error("no reason is provided")]
11pub struct SilentError;
12
13#[derive(Error, Debug, Clone)]
15#[error("{0}")]
16pub struct OtherError(String);
17
18#[derive(Debug, PartialEq, Eq, Clone, Copy, Display)]
19pub enum CommonErrorKind {
20 Pager,
21 Authorize,
22 Other,
24}
25
26impl ErrorCode for CommonErrorKind {
27 fn error_code(&self) -> u32 {
28 let parent_code = ErrorKind::error_code(&ErrorKind::Common);
29 match self {
30 CommonErrorKind::Pager => parent_code + SECOND_LEVEL_ERROR_CODE_INTERVAL * 1,
31 CommonErrorKind::Authorize => parent_code + SECOND_LEVEL_ERROR_CODE_INTERVAL * 2,
32 CommonErrorKind::Other => parent_code + SECOND_LEVEL_ERROR_CODE_INTERVAL * 3,
33 }
34 }
35}
36
37def_error_base_on_kind!(CommonError, CommonErrorKind, "Common error.");
38
39impl OtherError {
40 pub fn new<T>(reason: T) -> Self
42 where
43 T: fmt::Display,
44 {
45 Self(reason.to_string())
46 }
47}
48
49impl From<OtherError> for ErrorInfo {
50 fn from(value: OtherError) -> Self {
51 ErrorInfo {
52 code: CommonErrorKind::error_code(&CommonErrorKind::Other),
53 message: value.0,
54 }
55 }
56}
57
58impl From<OtherError> for CommonError {
59 fn from(error: OtherError) -> Self {
60 let error_info: ErrorInfo = error.into();
61 CommonErrorKind::Other.because(error_info)
62 }
63}