Add per-error HTTP status code
This commit is contained in:
parent
f70b5f8aa1
commit
09df210c17
2 changed files with 19 additions and 3 deletions
18
src/error.rs
18
src/error.rs
|
@ -20,6 +20,7 @@ use std::str::Utf8Error;
|
|||
|
||||
use hex::FromHexError;
|
||||
use hmac::digest::InvalidLength as HmacInvalidLength;
|
||||
use hyper::StatusCode;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum Error {
|
||||
|
@ -38,12 +39,27 @@ pub(crate) enum Error {
|
|||
Var(VarError),
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn status(&self) -> StatusCode {
|
||||
match self {
|
||||
Self::MethodMismatch(_) => StatusCode::METHOD_NOT_ALLOWED,
|
||||
Self::InvalidSecret => StatusCode::FORBIDDEN,
|
||||
Self::InvalidContentType => StatusCode::UNSUPPORTED_MEDIA_TYPE,
|
||||
Self::InvalidSignature => StatusCode::FORBIDDEN,
|
||||
Self::InvalidRequest => StatusCode::BAD_REQUEST,
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
Error::MethodMismatch(method) => write!(fmt, "method is invalid {method}, expected POST"),
|
||||
Error::MethodMismatch(method) => {
|
||||
write!(fmt, "method is invalid {method}, expected POST")
|
||||
}
|
||||
Error::InvalidSecret => write!(fmt, "the secret is invalid"),
|
||||
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
|
||||
Error::InvalidSignature => write!(fmt, "the signature is invalid"),
|
||||
|
|
|
@ -30,12 +30,12 @@ use tokio::sync::mpsc::UnboundedSender;
|
|||
|
||||
type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
fn error_res<E: std::fmt::Debug>(e: E) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||
fn error_res(e: Error) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||
error!("error response: {:?}", e);
|
||||
|
||||
let text = format!("{:?}", e);
|
||||
let res = Response::builder()
|
||||
.status(200)
|
||||
.status(e.status())
|
||||
.body(Full::new(Bytes::from(text)))
|
||||
.unwrap();
|
||||
Ok(res)
|
||||
|
|
Loading…
Reference in a new issue