b3-users 0.1.4

A simple user management system for the Internet Computer
Documentation
// 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,
    InvalidCanisterID,
    UserNotFound,
    UserAlreadyExists,
    PublicKeyMismatch,
    AccountNotFound,
    AccountAlreadyExists,
    ChainNotFound,
    ChainAlreadyExists,
    TransactionNotFound,
    AccountLimitReached,
    InvalidAccountKey,
    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::AccountNotFound => (
                RejectionCode::CanisterReject,
                "Account not found".to_string(),
            ),
            UserStateError::AccountAlreadyExists => (
                RejectionCode::CanisterReject,
                "Account 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::AccountLimitReached => (
                RejectionCode::CanisterReject,
                "Account limit reached".to_string(),
            ),
            UserStateError::InvalidAccountKey => (
                RejectionCode::CanisterReject,
                "Invalid account 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(),
            ),
            UserStateError::InvalidCanisterID => (
                RejectionCode::CanisterReject,
                "Invalid canister ID".to_string(),
            ),
        }
    }
}