1use serde::Serialize;
2use thiserror::Error;
3
4pub trait DataError: core::error::Error + core::fmt::Debug + core::fmt::Display + Serialize {
5 fn code(&self) -> i32;
6
7 fn msg(&self) -> &str;
8
9 fn data(&self) -> Option<&serde_json::Value>;
10
11 fn source(&self) -> Option<&(dyn core::error::Error + 'static)>;
12}
13
14pub type Result<T> = core::result::Result<T, Error>;
15
16#[derive(Debug, Error)]
17pub enum Error {
18 #[error("Decode base64 fail, string is {0}")]
20 FailToB64uDecode(String),
21
22 #[error("Parse date fail, data is {0}")]
23 DateFailParse(String),
24
25 #[error("Key fail.")]
26 KeyFail,
27
28 #[error("Password not match.")]
29 PwdNotMatching,
30
31 #[error("Missing env: {0}")]
32 MissingEnv(String),
33
34 #[error("Wrong format: {0}")]
35 WrongFormat(String),
36
37 #[error("Failed to set env: {0}, value: {1}, error: {2}")]
38 FailedToSetEnv(String, String, String),
39
40 #[error("Failed to remove env: {0}, error: {1}")]
41 FailedToRemoveEnv(String, String),
42}
43
44impl From<chrono::ParseError> for Error {
45 fn from(value: chrono::ParseError) -> Self {
46 Error::DateFailParse(value.to_string())
47 }
48}