b3_users/
error.rs

1// error.rs
2use ic_cdk::api::call::RejectionCode;
3use ic_cdk::export::candid::CandidType;
4
5/// Represents errors that can occur when working with the state.
6#[derive(CandidType, Debug)]
7pub enum UserStateError {
8    UnknownError,
9    InvalidData,
10    CallerNotAuthorized,
11    CallerIsNotOwner,
12    CallerIsNotWalletCanister,
13    InvalidCanisterID,
14    UserNotFound,
15    UserAlreadyExists,
16    PublicKeyMismatch,
17    AccountNotFound,
18    AccountAlreadyExists,
19    ChainNotFound,
20    ChainAlreadyExists,
21    TransactionNotFound,
22    AccountLimitReached,
23    InvalidAccountKey,
24    SettingNotFound,
25    PasswordHashError,
26    PasswordIsInvalid,
27    PasswordNotSet,
28}
29
30impl From<UserStateError> for (RejectionCode, String) {
31    fn from(error: UserStateError) -> Self {
32        match error {
33            UserStateError::UnknownError => {
34                (RejectionCode::CanisterReject, "Unknown error".to_string())
35            }
36            UserStateError::InvalidData => {
37                (RejectionCode::CanisterReject, "Invalid data".to_string())
38            }
39            UserStateError::CallerNotAuthorized => (
40                RejectionCode::CanisterReject,
41                "Caller not authorized to perform this action".to_string(),
42            ),
43            UserStateError::CallerIsNotOwner => (
44                RejectionCode::CanisterReject,
45                "Caller is not owner".to_string(),
46            ),
47            UserStateError::CallerIsNotWalletCanister => (
48                RejectionCode::CanisterReject,
49                "Caller is not wallet canister".to_string(),
50            ),
51            UserStateError::UserNotFound => {
52                (RejectionCode::CanisterReject, "User not found".to_string())
53            }
54            UserStateError::UserAlreadyExists => (
55                RejectionCode::CanisterReject,
56                "User already exists".to_string(),
57            ),
58            UserStateError::AccountNotFound => (
59                RejectionCode::CanisterReject,
60                "Account not found".to_string(),
61            ),
62            UserStateError::AccountAlreadyExists => (
63                RejectionCode::CanisterReject,
64                "Account already exists".to_string(),
65            ),
66            UserStateError::ChainNotFound => {
67                (RejectionCode::CanisterReject, "Chain not found".to_string())
68            }
69            UserStateError::ChainAlreadyExists => (
70                RejectionCode::CanisterReject,
71                "Chain already exists".to_string(),
72            ),
73            UserStateError::TransactionNotFound => (
74                RejectionCode::CanisterReject,
75                "Transaction not found".to_string(),
76            ),
77            UserStateError::AccountLimitReached => (
78                RejectionCode::CanisterReject,
79                "Account limit reached".to_string(),
80            ),
81            UserStateError::InvalidAccountKey => (
82                RejectionCode::CanisterReject,
83                "Invalid account key".to_string(),
84            ),
85            UserStateError::SettingNotFound => (
86                RejectionCode::CanisterReject,
87                "Setting not found".to_string(),
88            ),
89            UserStateError::PasswordHashError => (
90                RejectionCode::CanisterReject,
91                "Password hash error".to_string(),
92            ),
93            UserStateError::PasswordIsInvalid => (
94                RejectionCode::CanisterReject,
95                "Password is invalid".to_string(),
96            ),
97
98            UserStateError::PasswordNotSet => (
99                RejectionCode::CanisterReject,
100                "Password not set".to_string(),
101            ),
102            UserStateError::PublicKeyMismatch => (
103                RejectionCode::CanisterReject,
104                "Public key mismatch".to_string(),
105            ),
106            UserStateError::InvalidCanisterID => (
107                RejectionCode::CanisterReject,
108                "Invalid canister ID".to_string(),
109            ),
110        }
111    }
112}