Skip to content

Commit

Permalink
clone gitlab errors into marketstack
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenwong97 committed Sep 25, 2023
1 parent d4e5a05 commit 8127979
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ reqwest = { version = "~0.11.19", features = [
"blocking",
"json",
], default-features = false, optional = true }
thiserror = { version = "^1.0.2", optional = true }
thiserror = { version = "^1.0.48", optional = true }
http = "~0.2"
serde = { version = "~1.0.103", features = ["derive"] }
serde_json = "^1.0.25"
serde_urlencoded = "~0.7"
url = "^2.1"
bytes = "1.5.0"

[dev-dependencies]
dotenvy = "0.15.7"
64 changes: 64 additions & 0 deletions src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file may not be copied, modified, or distributed
// except according to those terms.

use std::any;
use std::error::Error;

use thiserror::Error;
Expand Down Expand Up @@ -90,3 +91,66 @@ where
},
// TODO: implement pagination error
}

impl<E> ApiError<E>
where
E: Error + Send + Sync + 'static,
{
/// Create an API error in a client error.
pub fn client(source: E) -> Self {
ApiError::Client { source }
}

/// Wrap a client error in another wrapper.
pub fn map_client<F, W>(self, f: F) -> ApiError<W>
where
F: FnOnce(E) -> W,
W: Error + Send + Sync + 'static,
{
match self {
Self::Client { source } => ApiError::client(f(source)),
Self::UrlParse { source } => ApiError::UrlParse { source },
Self::Body { source } => ApiError::Body { source },
Self::Json { source } => ApiError::Json { source },
Self::Marketstack { msg } => ApiError::Marketstack { msg },
Self::MarketstackService { status, data } => {
ApiError::MarketstackService { status, data }
}
Self::MarketstackObject { obj } => ApiError::MarketstackObject { obj },
Self::MarketstackUnrecognized { obj } => ApiError::MarketstackUnrecognized { obj },
Self::DataType { source, typename } => ApiError::DataType { source, typename },
}
}

pub(crate) fn server_error(status: http::StatusCode, body: &bytes::Bytes) -> Self {
Self::MarketstackService {
status,
data: body.into_iter().copied().collect(),
}
}

pub(crate) fn from_marketstack(value: serde_json::Value) -> Self {
let error_value = value
.pointer("/message")
.or_else(|| value.pointer("/error"));

if let Some(error_value) = error_value {
if let Some(msg) = error_value.as_str() {
ApiError::Marketstack { msg: msg.into() }
} else {
ApiError::MarketstackObject {
obj: error_value.clone(),
}
}
} else {
ApiError::MarketstackUnrecognized { obj: value }
}
}

pub(crate) fn data_type<T>(source: serde_json::Error) -> Self {
ApiError::DataType {
source,
typename: any::type_name::<T>(),
}
}
}

0 comments on commit 8127979

Please sign in to comment.