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
103
104
105
106
107
108
109
110
111
use miette::{Diagnostic, NamedSource, SourceOffset};
use reqwest::Url;
use thiserror::Error;

#[derive(Debug)]
pub struct Response(Option<String>);

#[derive(Debug, Error, Diagnostic)]
pub enum OroClientError {
    /// An invalid URL was provided.
    #[error(transparent)]
    #[diagnostic(code(oro_client::url_parse_error), url(docsrs))]
    UrlParseError(#[from] url::ParseError),

    /// The package was not found in the registry.
    ///
    /// Make sure the package name is spelled correctly and that you've
    /// configured the right registry to fetch it from.
    #[error("Package `{1}` was not found in registry {0}.")]
    #[diagnostic(code(oro_client::package_not_found), url(docsrs))]
    PackageNotFound(Url, String),

    /// Got some bad JSON we couldn't parse.
    #[error("Received some unexpected JSON. Unable to parse.")]
    #[diagnostic(code(oro_client::bad_json), url(docsrs))]
    BadJson {
        source: serde_json::Error,
        url: String,
        #[source_code]
        json: NamedSource,
        #[label("here")]
        err_loc: (usize, usize),
    },

    /// A generic request error happened while making a request. Refer to the
    /// error message for more details.
    #[error(transparent)]
    #[diagnostic(code(oro_client::request_error), url(docsrs))]
    RequestError(#[from] reqwest::Error),

    /// Recived unexpected response.
    #[error("Received unexpected response. \n {0}")]
    #[diagnostic(code(oro_client::response_error), url(docsrs))]
    ResponseError(Response),

    /// No such user.
    #[error("No such user. (provided username: {0})")]
    #[diagnostic(code(oro_client::no_such_user_error), url(docsrs))]
    NoSuchUserError(String),

    /// Incorrect or missing password.
    #[error("Incorrect or missing password.")]
    #[diagnostic(code(oro_client::incorrect_password_error), url(docsrs))]
    IncorrectPasswordError,

    /// Unable to authenticate, your authentication token seems to be invalid.
    #[error("Unable to authenticate, your authentication token seems to be invalid.")]
    #[diagnostic(code(oro_client::invalid_token_error), url(docsrs))]
    InvalidTokenError,

    /// This operation requires a one-time password from your authenticator.
    #[error("This operation requires a one-time password from your authenticator.")]
    #[diagnostic(code(oro_client::otp_required_error), url(docsrs))]
    OTPRequiredError,

    /// A generic request middleware error happened while making a request.
    /// Refer to the error message for more details.
    #[error(transparent)]
    #[diagnostic(code(oro_client::request_middleware_error), url(docsrs))]
    RequestMiddlewareError(#[from] reqwest_middleware::Error),
}

impl OroClientError {
    pub fn from_json_err(err: serde_json::Error, url: String, json: String) -> Self {
        // These json strings can get VERY LONG and miette doesn't (yet?)
        // support any "windowing" mechanism for displaying stuff, so we have
        // to manually shorten the string to only the relevant bits and
        // translate the spans accordingly.
        let err_offset = SourceOffset::from_location(&json, err.line(), err.column());
        let json_len = json.len();
        let local_offset = err_offset.offset().saturating_sub(40);
        let local_len = std::cmp::min(40, json_len - err_offset.offset());
        let snipped_json = json[local_offset..err_offset.offset() + local_len].to_string();
        Self::BadJson {
            source: err,
            url: url.clone(),
            json: NamedSource::new(url, snipped_json),
            err_loc: (err_offset.offset() - local_offset, 0),
        }
    }
}

impl From<Option<String>> for Response {
    fn from(value: Option<String>) -> Self {
        Response(value)
    }
}

impl std::fmt::Display for Response {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "{}",
            if let Some(response) = &self.0 {
                response
            } else {
                ""
            }
        )
    }
}