top_gg/error.rs
1//! Error enum with all possible reasons for errors and wrapping Result type.
2
3use reqwest::{
4 header::{HeaderName, InvalidHeaderValue as ReqwestInvalidHeaderValue},
5 Error as ReqwestError,
6};
7use serde_json::Error as JsonError;
8use snafu::Snafu;
9use std::result::Result as StdResult;
10use url::ParseError as UrlParseError;
11
12/// A result type to compose a successful value and the library's [`Error`]
13/// type.
14///
15/// [`Error`]: enum.Error.html
16pub type Result<T> = StdResult<T, Error>;
17
18/// An error type to compose a singular error enum between various dependencies'
19/// errors.
20#[derive(Debug, Snafu)]
21#[snafu(visibility(pub(crate)))]
22pub enum Error {
23 /// The response body couldn't be chunked as a UTF-8 valid string.
24 ChunkingText {
25 /// The reason for the error.
26 source: ReqwestError,
27 },
28 /// An error from the `serde_json` crate.
29 ///
30 /// A potential reason for this is when there is an error deserializing a
31 /// JSON response body.
32 Deserializing {
33 /// The reason for the error.
34 source: JsonError,
35 /// The payload that couldn't be deserialized.
36 text: String,
37 },
38 /// A header value was invalid.
39 InvalidHeaderValue {
40 /// The name of the header.
41 name: HeaderName,
42 /// The reason for the error.
43 source: ReqwestInvalidHeaderValue,
44 /// The value that couldn't be parsed.
45 value: String,
46 },
47 /// The formatted URL is invalid.
48 ///
49 /// This is likely a library issue.
50 InvalidUrl {
51 /// The reason for the error.
52 source: UrlParseError,
53 /// The URI that couldn't be parsed.
54 uri: String,
55 },
56 /// An error from the `reqwest` crate.
57 Request {
58 /// The reason for the error.
59 source: ReqwestError,
60 },
61 /// A token is needed for this request but wasn't present.
62 TokenMissing,
63}
64
65impl From<ReqwestError> for Error {
66 fn from(source: ReqwestError) -> Self {
67 Self::Request { source }
68 }
69}