aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs55
1 files changed, 34 insertions, 21 deletions
diff --git a/src/error.rs b/src/error.rs
index 5f35da2..fe998d0 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,55 +1,68 @@
-use std::fmt::Display;
+//! Provides the [`crate::Error`] type.
+use thiserror::Error;
use hyper::StatusCode;
/// Encapsulates a status code and an error message.
-///
-/// All client errors in [`crate::request`] implement [`Into<Error>`].
-#[derive(Debug)]
-pub struct Error {
+#[derive(Error, Debug)]
+#[error("SimpleError({code}, {message})")]
+pub struct SimpleError {
pub code: StatusCode,
pub message: String,
}
-impl Display for Error {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "error: {}", self.message)
+impl SimpleError {
+ /// Returns an HTTP response builder with the status code set to `self.code`.
+ pub fn response_builder(&self) -> hyper::http::response::Builder {
+ hyper::Response::builder().status(self.code)
}
}
-impl std::error::Error for Error {
+/// Error type for request handlers.
+///
+/// All client errors in [`crate::request`] implement [`Into<Error::Simple>`].
+#[derive(Error, Debug)]
+pub enum Error {
+ #[error("SimpleError({}, {})", .0.code, .0.message)]
+ Simple(#[from] SimpleError),
+
+ #[error("ResponseError({})", .0.status())]
+ Response(hyper::Response<hyper::Body>),
}
impl Error {
- /// Returns an HTTP response builder with the status code set to `self.code`.
- pub fn response_builder(&self) -> hyper::http::response::Builder {
- hyper::Response::builder().status(self.code)
- }
+ // some convenience methods
- // some conventient constructors for common errors
+ pub fn simple(code: StatusCode, message: String) -> Self {
+ Error::Simple(SimpleError{code, message})
+ }
pub fn bad_request(message: String) -> Self {
- Error{code: StatusCode::BAD_REQUEST, message}
+ Error::Simple(SimpleError{code: StatusCode::BAD_REQUEST, message})
}
+
pub fn not_found(message: String) -> Self {
- Error{code: StatusCode::NOT_FOUND, message}
+ Error::Simple(SimpleError{code: StatusCode::NOT_FOUND, message})
}
+
pub fn unauthorized(message: String) -> Self {
- Error{code: StatusCode::UNAUTHORIZED, message}
+ Error::Simple(SimpleError{code: StatusCode::UNAUTHORIZED, message})
}
+
pub fn internal(message: String) -> Self {
- Error{code: StatusCode::INTERNAL_SERVER_ERROR, message}
+ Error::Simple(SimpleError{code: StatusCode::INTERNAL_SERVER_ERROR, message})
}
+
pub fn method_not_allowed(message: String) -> Self {
- Error{code: StatusCode::METHOD_NOT_ALLOWED, message}
+ Error::Simple(SimpleError{code: StatusCode::METHOD_NOT_ALLOWED, message})
}
}
-macro_rules! impl_into_http_error {
+macro_rules! impl_into_error_simple {
($type:ident, $status:expr) => {
impl From<$type> for crate::error::Error {
fn from(err: $type) -> Self {
- Self{code: $status, message: format!("{}", err)}
+ Self::Simple(crate::error::SimpleError{code: $status, message: format!("{}", err)})
}
}
};