#![allow(unused)]
use thiserror::Error;
use std::fmt::{Display, Write};
use crate::schema::WebError;
#[derive(Error, Debug)]
#[error(transparent)]
pub enum ClientError {
ConnectionError(#[from] ConnectionError),
ParameterError(#[from] ParameterError),
RequestFailedError(#[from] RequestFailedError),
AuthenticationError(#[from] AuthenticationError),
ResourceNotFoundError(#[from] ResourceNotFoundError),
ServerError(#[from] ServerError),
UnknownError(#[from] UnknownError),
}
impl From<reqwest::Error> for ClientError {
fn from(value: reqwest::Error) -> Self {
if value.is_connect() {
return ConnectionError::new(Box::new(value)).into();
}
if value.is_request() || value.is_timeout() {
return RequestFailedError::new(Box::new(value)).into();
}
UnknownError::new(Box::new(value)).into()
}
}
impl From<serde_json::Error> for ClientError {
fn from(value: serde_json::Error) -> Self {
RequestFailedError::new(Box::new(value)).into()
}
}
#[derive(Debug, thiserror::Error)]
pub struct RequestFailedError {
#[source]
source: Box<dyn std::error::Error>,
}
impl RequestFailedError {
pub(crate) fn new(source: Box<dyn std::error::Error>) -> Self {
Self { source }
}
}
impl Display for RequestFailedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "request to server failed")
}
}
impl From<Vec<WebError>> for RequestFailedError {
fn from(value: Vec<WebError>) -> Self {
let mut error_string = String::new();
for error in value {
writeln!(&mut error_string, "{}", error);
}
RequestFailedError::new(error_string.into())
}
}
#[derive(Debug, thiserror::Error)]
pub struct ConnectionError {
#[source]
source: Box<dyn std::error::Error>,
}
impl ConnectionError {
pub(crate) fn new(source: Box<dyn std::error::Error>) -> Self {
Self { source }
}
}
impl Display for ConnectionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "connection to server failed")
}
}
impl From<Vec<WebError>> for ConnectionError {
fn from(value: Vec<WebError>) -> Self {
let mut error_string = String::new();
for error in value {
writeln!(&mut error_string, "{}", error);
}
ConnectionError::new(error_string.into())
}
}
#[derive(Debug, thiserror::Error)]
pub struct ParameterError {
#[source]
source: Box<dyn std::error::Error>,
}
impl ParameterError {
pub(crate) fn new(source: Box<dyn std::error::Error>) -> Self {
Self { source }
}
}
impl Display for ParameterError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "provided parameters caused an error")
}
}
impl From<Vec<WebError>> for ParameterError {
fn from(value: Vec<WebError>) -> Self {
let mut error_string = String::new();
for error in value {
writeln!(&mut error_string, "{}", error);
}
ParameterError::new(error_string.into())
}
}
#[derive(Debug, thiserror::Error)]
pub struct AuthenticationError {
#[source]
source: Box<dyn std::error::Error>,
}
impl AuthenticationError {
pub(crate) fn new(source: Box<dyn std::error::Error>) -> Self {
Self { source }
}
}
impl Display for AuthenticationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "could not authenticate")
}
}
impl From<Vec<WebError>> for AuthenticationError {
fn from(value: Vec<WebError>) -> Self {
let mut error_string = String::new();
for error in value {
writeln!(&mut error_string, "{}", error);
}
AuthenticationError::new(error_string.into())
}
}
#[derive(Debug, thiserror::Error)]
pub struct ResourceNotFoundError {
#[source]
source: Box<dyn std::error::Error>,
}
impl ResourceNotFoundError {
pub(crate) fn new(source: Box<dyn std::error::Error>) -> Self {
Self { source }
}
}
impl Display for ResourceNotFoundError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "requested resource could not be found")
}
}
impl From<Vec<WebError>> for ResourceNotFoundError {
fn from(value: Vec<WebError>) -> Self {
let mut error_string = String::new();
for error in value {
writeln!(&mut error_string, "{}", error);
}
ResourceNotFoundError::new(error_string.into())
}
}
#[derive(Debug, thiserror::Error)]
pub struct ServerError {
#[source]
source: Box<dyn std::error::Error>,
}
impl ServerError {
pub(crate) fn new(source: Box<dyn std::error::Error>) -> Self {
Self { source }
}
}
impl Display for ServerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "requested resource could not be found")
}
}
impl From<Vec<WebError>> for ServerError {
fn from(value: Vec<WebError>) -> Self {
let mut error_string = String::new();
for error in value {
writeln!(&mut error_string, "{}", error);
}
ServerError::new(error_string.into())
}
}
#[derive(Debug, thiserror::Error)]
pub struct UnknownError {
#[source]
source: Box<dyn std::error::Error>,
}
impl UnknownError {
pub(crate) fn new(source: Box<dyn std::error::Error>) -> Self {
Self { source }
}
}
impl Display for UnknownError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "error not recognised")
}
}
impl From<Vec<WebError>> for UnknownError {
fn from(value: Vec<WebError>) -> Self {
let mut error_string = String::new();
for error in value {
writeln!(&mut error_string, "{}", error);
}
UnknownError::new(error_string.into())
}
}