Skip to content

Commit

Permalink
layout error enums
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenwong97 committed Sep 25, 2023
1 parent c464f9d commit d4e5a05
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ serde = { version = "~1.0.103", features = ["derive"] }
serde_json = "^1.0.25"
serde_urlencoded = "~0.7"
url = "^2.1"

[dev-dependencies]
dotenvy = "0.15.7"
1 change: 1 addition & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod endpoint;
mod error;
mod query;

pub use self::error::BodyError;
73 changes: 73 additions & 0 deletions src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This file may not be copied, modified, or distributed
// except according to those terms.

use std::error::Error;

use thiserror::Error;

/// Errors which may occur when creating form data.
Expand All @@ -17,3 +19,74 @@ pub enum BodyError {
source: serde_urlencoded::ser::Error,
},
}

/// Errors which may occur when using API endpoints.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ApiError<E>
where
E: Error + Send + Sync + 'static,
{
/// The client encountered an error.
#[error("client error: {}", source)]
Client {
/// The client error.
source: E,
},
/// The URL failed to parse.
#[error("failed to parse URL: {}", source)]
UrlParse {
/// The source of the error.
#[from]
source: url::ParseError,
},
/// Body data could not be created.
#[error("failed to create request body: {}", source)]
Body {
/// The source of the error.
#[from]
source: BodyError,
},
/// JSON deserialization from Marketstack failed.
#[error("could not parse JSON response: {}", source)]
Json {
/// The source of the error.
#[from]
source: serde_json::Error,
},
/// Marketstack returned an error message.
#[error("marketstack server error: {}", msg)]
Marketstack {
/// The error message from Marketstack.
msg: String,
},
/// Marketstack returned an error without JSON information.
#[error("marketstack internal server error: {}", status)]
MarketstackService {
/// The status code for the return.
status: http::StatusCode,
/// The error data from Marketstack.
data: Vec<u8>,
},
/// Marketstack returned an error object.
#[error("marketstack server error: {:?}", obj)]
MarketstackObject {
/// The error object from Marketstack.
obj: serde_json::Value,
},
/// Marketstack returned an HTTP error with JSON we did not recognize.
#[error("marketstack server error: {:?}", obj)]
MarketstackUnrecognized {
/// The full object from Marketstack.
obj: serde_json::Value,
},
/// Failed to parse an expected data type from JSON.
#[error("could not parse {} data from JSON: {}", typename, source)]
DataType {
/// The source of the error.
source: serde_json::Error,
/// The name of the type that could not be deserialized.
typename: &'static str,
},
// TODO: implement pagination error
}
4 changes: 4 additions & 0 deletions src/api/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed
// except according to those terms.

0 comments on commit d4e5a05

Please sign in to comment.