spring/
error.rs

1use std::io::{self, ErrorKind};
2use thiserror::Error;
3
4/// Spring custom error type
5#[derive(Error, Debug)]
6pub enum AppError {
7    /// component not exists
8    #[error("{0} component not exists")]
9    ComponentNotExist(&'static str),
10
11    /// `.env` file reading failed
12    #[error(transparent)]
13    EnvError(#[from] dotenvy::Error),
14
15    /// File IO Error
16    #[error(transparent)]
17    IOError(#[from] io::Error),
18
19    /// toml file parsing error
20    #[error(transparent)]
21    TomlParseError(#[from] toml::de::Error),
22
23    /// Configuration merge error in toml file
24    #[error("merge toml error: {0}")]
25    TomlMergeError(String),
26
27    /// tokio asynchronous task join failed
28    #[error(transparent)]
29    JoinError(#[from] tokio::task::JoinError),
30
31    /// Deserialization of configuration in toml file to rust struct failed
32    #[error("Failed to deserialize the configuration of prefix \"{0}\": {1}")]
33    DeserializeErr(&'static str, toml::de::Error),
34
35    /// Other runtime errors
36    #[error(transparent)]
37    OtherError(#[from] anyhow::Error),
38}
39
40impl AppError {
41    /// Failed to read file io
42    pub fn from_io(kind: ErrorKind, msg: &str) -> Self {
43        AppError::IOError(io::Error::new(kind, msg))
44    }
45}
46
47/// Contains the return value of AppError
48pub type Result<T> = std::result::Result<T, AppError>;