1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// error.rs
use ic_cdk::api::call::RejectionCode;
use ic_cdk::export::candid::CandidType;

/// Represents errors that can occur when working with the state.
#[derive(CandidType, Debug)]
pub enum UserStateError {
    UnknownError,
    InvalidData,
    CallerNotAuthorized,
    CallerIsNotOwner,
    CallerIsNotWalletCanister,
    UserNotFound,
    UserAlreadyExists,
    PublicKeyMismatch,
    AddressNotFound,
    AddressAlreadyExists,
    ChainNotFound,
    ChainAlreadyExists,
    TransactionNotFound,
    AddressLimitReached,
    InvalidAddressKey,
    SettingNotFound,
    PasswordHashError,
    PasswordIsInvalid,
    PasswordNotSet,
}

impl From<UserStateError> for (RejectionCode, String) {
    fn from(error: UserStateError) -> Self {
        match error {
            UserStateError::UnknownError => {
                (RejectionCode::CanisterReject, "Unknown error".to_string())
            }
            UserStateError::InvalidData => {
                (RejectionCode::CanisterReject, "Invalid data".to_string())
            }
            UserStateError::CallerNotAuthorized => (
                RejectionCode::CanisterReject,
                "Caller not authorized to perform this action".to_string(),
            ),
            UserStateError::CallerIsNotOwner => (
                RejectionCode::CanisterReject,
                "Caller is not owner".to_string(),
            ),
            UserStateError::CallerIsNotWalletCanister => (
                RejectionCode::CanisterReject,
                "Caller is not wallet canister".to_string(),
            ),
            UserStateError::UserNotFound => {
                (RejectionCode::CanisterReject, "User not found".to_string())
            }
            UserStateError::UserAlreadyExists => (
                RejectionCode::CanisterReject,
                "User already exists".to_string(),
            ),
            UserStateError::AddressNotFound => (
                RejectionCode::CanisterReject,
                "Address not found".to_string(),
            ),
            UserStateError::AddressAlreadyExists => (
                RejectionCode::CanisterReject,
                "Address already exists".to_string(),
            ),
            UserStateError::ChainNotFound => {
                (RejectionCode::CanisterReject, "Chain not found".to_string())
            }
            UserStateError::ChainAlreadyExists => (
                RejectionCode::CanisterReject,
                "Chain already exists".to_string(),
            ),
            UserStateError::TransactionNotFound => (
                RejectionCode::CanisterReject,
                "Transaction not found".to_string(),
            ),
            UserStateError::AddressLimitReached => (
                RejectionCode::CanisterReject,
                "Address limit reached".to_string(),
            ),
            UserStateError::InvalidAddressKey => (
                RejectionCode::CanisterReject,
                "Invalid address key".to_string(),
            ),
            UserStateError::SettingNotFound => (
                RejectionCode::CanisterReject,
                "Setting not found".to_string(),
            ),
            UserStateError::PasswordHashError => (
                RejectionCode::CanisterReject,
                "Password hash error".to_string(),
            ),
            UserStateError::PasswordIsInvalid => (
                RejectionCode::CanisterReject,
                "Password is invalid".to_string(),
            ),

            UserStateError::PasswordNotSet => (
                RejectionCode::CanisterReject,
                "Password not set".to_string(),
            ),
            UserStateError::PublicKeyMismatch => (
                RejectionCode::CanisterReject,
                "Public key mismatch".to_string(),
            ),
        }
    }
}