Extract Error to error module

This commit is contained in:
Koen Bolhuis 2021-01-10 19:30:03 +01:00
parent a3265dceac
commit 14428576a8
2 changed files with 53 additions and 50 deletions

50
src/error.rs Normal file
View File

@ -0,0 +1,50 @@
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}")]
Api { code: u16, error: String },
/// 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")]
Http(#[source] ureq::Error),
}
#[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),
},
ureq::Error::Transport(_) => Error::Http(error),
}
}
}

View File

@ -1,68 +1,21 @@
//! API bindings for ListenBrainz.
use std::io;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde::Serialize;
use ureq::Agent;
mod endpoint;
mod error;
pub mod models;
use endpoint::Endpoint;
pub use error::Error;
use models::request::*;
use models::response::*;
const API_ROOT_URL: &str = "https://api.listenbrainz.org/1/";
/// 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}")]
Api { code: u16, error: String },
/// 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")]
Http(#[source] ureq::Error),
}
#[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),
},
ureq::Error::Transport(_) => Error::Http(error),
}
}
}
/// Low-level client that more-or-less directly wraps the ListenBrainz HTTP API.
///
/// Client exposes functions that map one-to-one to the API methods described