Skip to content

Commit

Permalink
add TCError::Unavailable variant
Browse files Browse the repository at this point in the history
  • Loading branch information
haydnv committed Apr 15, 2022
1 parent 4cff95c commit e17325b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
35 changes: 23 additions & 12 deletions host/error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum ErrorType {
NotImplemented,
Timeout,
Unauthorized,
Unavailable,
}

impl<'en> en::IntoStream<'en> for ErrorType {
Expand All @@ -38,6 +39,7 @@ impl<'en> en::IntoStream<'en> for ErrorType {
Self::NotImplemented => "not_implemented",
Self::Timeout => "timeout",
Self::Unauthorized => "unauthorized",
Self::Unavailable => "temporarily unavailable",
}
)
.into_stream(encoder)
Expand All @@ -52,18 +54,19 @@ impl fmt::Debug for ErrorType {

impl fmt::Display for ErrorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::BadGateway => f.write_str("bad gateway"),
Self::BadRequest => f.write_str("bad request"),
Self::Conflict => f.write_str("conflict"),
Self::Forbidden => f.write_str("forbidden"),
Self::Internal => f.write_str("internal error"),
Self::MethodNotAllowed => f.write_str("method not allowed"),
Self::NotFound => f.write_str("not found"),
Self::NotImplemented => f.write_str("not implemented"),
Self::Timeout => f.write_str("request timeout"),
Self::Unauthorized => f.write_str("unauthorized"),
}
f.write_str(match self {
Self::BadGateway => "bad gateway",
Self::BadRequest => "bad request",
Self::Conflict => "conflict",
Self::Forbidden => "forbidden",
Self::Internal => "internal error",
Self::MethodNotAllowed => "method not allowed",
Self::NotFound => "not found",
Self::NotImplemented => "not implemented",
Self::Timeout => "request timeout",
Self::Unauthorized => "unauthorized",
Self::Unavailable => "temporarily unavailable",
})
}
}

Expand Down Expand Up @@ -170,6 +173,14 @@ impl TCError {
}
}

/// Error indicating that this host is currently overloaded
pub fn unavailable<I: fmt::Display>(info: I) -> Self {
Self {
code: ErrorType::Unavailable,
message: info.to_string(),
}
}

/// Error indicating that the request is badly-constructed or nonsensical.
pub fn unsupported<I: fmt::Display>(info: I) -> Self {
Self {
Expand Down
1 change: 1 addition & 0 deletions host/src/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ fn transform_error(err: TCError, encoding: Encoding) -> hyper::Response<Body> {
NotImplemented => StatusCode::NOT_IMPLEMENTED,
Timeout => StatusCode::REQUEST_TIMEOUT,
Unauthorized => StatusCode::UNAUTHORIZED,
Unavailable => StatusCode::SERVICE_UNAVAILABLE,
};

let body = match encoding {
Expand Down

0 comments on commit e17325b

Please sign in to comment.