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
use std::fmt::Display;

use anyhow::{Context, Result};
use http::StatusCode;
use thiserror::Error;

/// novel-api error
#[must_use]
#[derive(Debug, Error)]
pub enum Error {
    #[error(transparent)]
    StdIo(#[from] std::io::Error),
    #[error(transparent)]
    StdSystemTime(#[from] std::time::SystemTimeError),
    #[error(transparent)]
    StdParseInt(#[from] std::num::ParseIntError),
    #[error(transparent)]
    Anyhow(#[from] anyhow::Error),
    #[error(transparent)]
    Reqwest(#[from] reqwest::Error),
    #[error(transparent)]
    Boring(#[from] boring::error::ErrorStack),
    #[error(transparent)]
    Base64Simd(#[from] base64_simd::Error),
    #[error(transparent)]
    SerdeJson(#[from] serde_json::Error),
    #[error(transparent)]
    Opener(#[from] opener::OpenError),
    #[error(transparent)]
    Semver(#[from] semver::Error),
    #[error(transparent)]
    Confy(#[from] confy::ConfyError),
    #[error(transparent)]
    Simdutf8(#[from] simdutf8::basic::Utf8Error),
    #[error(transparent)]
    SeaOrm(#[from] sea_orm::DbErr),
    #[error(transparent)]
    Chrono(#[from] chrono::ParseError),
    #[error(transparent)]
    Image(#[from] image::ImageError),
    #[error(transparent)]
    Keyring(#[from] keyring::Error),
    #[error(transparent)]
    Url(#[from] url::ParseError),
    #[error(transparent)]
    Cookie(#[from] cookie_store::CookieError),
    #[error(transparent)]
    CookieStore(#[from] cookie_store::Error),
    #[error(transparent)]
    StatusCode(#[from] http::status::InvalidStatusCode),
    #[error("{0}")]
    NovelApi(String),
    #[error("The HTTP request was unsuccessful, status code: `{code}`, message: `{msg}`")]
    Http { code: StatusCode, msg: String },
}

/// Source code location
#[must_use]
pub struct Location {
    pub file: &'static str,
    pub function_name: &'static str,
    pub line: u32,
    pub column: u32,
}

/// Add source code location
pub trait ErrorLocation<T, E> {
    fn location(self, loc: Location) -> Result<T>;
}

impl<T, E> ErrorLocation<T, E> for Result<T, E>
where
    E: Display,
    Result<T, E>: Context<T, E>,
{
    fn location(self, loc: Location) -> Result<T> {
        let msg = self.as_ref().err().map(ToString::to_string);
        self.with_context(|| {
            format!(
                "{}, in function `{}`, at `{}:{}:{}`",
                msg.unwrap(),
                loc.function_name,
                loc.file,
                loc.line,
                loc.column,
            )
        })
    }
}

/// Macros for creating source code location
#[macro_export]
macro_rules! here {
    () => {
        Location {
            file: file!(),
            function_name: stdext::function_name!(),
            line: line!(),
            column: column!(),
        }
    };
}