| 1 | use serde_repr::{Deserialize_repr, Serialize_repr}; |
| 2 | use ts_rs::TS; |
| 3 | |
| 4 | #[derive(TS)] |
| 5 | #[ts(export)] |
| 6 | #[derive(Serialize_repr, Deserialize_repr)] |
| 7 | #[repr(i32)] |
| 8 | #[derive(Debug)] |
| 9 | pub enum ErrorCode { |
| 10 | // User defined error codes starts from 1000 |
| 11 | UserNotFound = 1000, |
| 12 | InvalidCredentials = 1001, |
| 13 | |
| 14 | // Generic error codes starts from 1 |
| 15 | InvalidRequest = 1, |
| 16 | ParseError = 2, |
| 17 | NetworkError = 3, |
| 18 | } |
| 19 | |
| 20 | impl ErrorCode { |
| 21 | pub fn error_message(&self) -> &str { |
| 22 | match self { |
| 23 | ErrorCode::UserNotFound => "User not found.", |
| 24 | ErrorCode::InvalidRequest => "Invalid request.", |
| 25 | ErrorCode::ParseError => "Failed to parse response.", |
| 26 | ErrorCode::NetworkError => "Failed to get response.", |
| 27 | ErrorCode::InvalidCredentials => "Invalid credentials.", |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | } |