2021-01-10 18:30:03 +00:00
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
/// Represents errors that can occor while interacting with the API.
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum Error {
|
|
|
|
/// The API returned a non-200 status code.
|
|
|
|
#[error("API error ({code}): {error}")]
|
2021-01-13 12:05:50 +00:00
|
|
|
Api {
|
|
|
|
/// The HTTP status code.
|
|
|
|
code: u16,
|
|
|
|
|
|
|
|
/// A message describing the error.
|
2021-01-13 13:28:59 +00:00
|
|
|
error: String,
|
2021-01-13 12:05:50 +00:00
|
|
|
},
|
2021-01-10 18:30:03 +00:00
|
|
|
|
|
|
|
/// The input data could not be converted into JSON.
|
|
|
|
#[error("could not convert request input into JSON")]
|
|
|
|
RequestJson(#[source] serde_json::Error),
|
|
|
|
|
|
|
|
/// The HTTP response could not be converted into JSON.
|
|
|
|
#[error("could not convert HTTP response into JSON")]
|
|
|
|
ResponseJson(#[source] io::Error),
|
|
|
|
|
|
|
|
/// There was some other HTTP error while interacting with the API.
|
|
|
|
#[error("HTTP error")]
|
2021-01-13 13:28:28 +00:00
|
|
|
Http(#[source] Box<ureq::Error>),
|
2021-01-19 23:16:53 +00:00
|
|
|
|
2021-01-19 23:52:10 +00:00
|
|
|
/// The token that was attempted to be used for authentication is invalid.
|
|
|
|
#[error("invalid authentication token")]
|
|
|
|
InvalidToken,
|
|
|
|
|
2021-01-19 23:16:53 +00:00
|
|
|
/// Tried to access a service that requires authentication.
|
|
|
|
#[error("not authenticated")]
|
|
|
|
NotAuthenticated,
|
2021-01-10 18:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct ApiError {
|
|
|
|
code: u16,
|
|
|
|
error: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ApiError> for Error {
|
|
|
|
fn from(api_error: ApiError) -> Self {
|
|
|
|
Error::Api {
|
|
|
|
code: api_error.code,
|
|
|
|
error: api_error.error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ureq::Error> for Error {
|
|
|
|
fn from(error: ureq::Error) -> Self {
|
|
|
|
match error {
|
|
|
|
ureq::Error::Status(_code, response) => match response.into_json::<ApiError>() {
|
|
|
|
Ok(api_error) => api_error.into(),
|
|
|
|
Err(err) => Error::ResponseJson(err),
|
|
|
|
},
|
2021-01-13 13:28:28 +00:00
|
|
|
ureq::Error::Transport(_) => Error::Http(Box::new(error)),
|
2021-01-10 18:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|