split error module

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-05-20 16:27:34 +02:00
parent b8aefb0159
commit 5350386133
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2
2 changed files with 65 additions and 45 deletions

60
src/error.rs Normal file
View file

@ -0,0 +1,60 @@
// Copyright (C) 2023-2099 The crate authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::error::Error as StdError;
use std::str::Utf8Error;
#[derive(Debug)]
pub(crate) enum Error {
MethodMismatch,
InvalidToken,
InvalidContentType,
Hyper(hyper::Error),
SerdeJson(serde_json::Error),
Utf8(Utf8Error),
}
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 => write!(fmt, "the method is invalid"),
Error::InvalidToken => write!(fmt, "the token is invalid"),
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e),
Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e),
}
}
}
impl From<hyper::Error> for Error {
fn from(err: hyper::Error) -> Error {
Error::Hyper(err)
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
Error::SerdeJson(err)
}
}
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Error {
Error::Utf8(err)
}
}

View file

@ -15,10 +15,13 @@
#![feature(let_chains)]
mod error;
use crate::error::Error;
use std::convert::Infallible;
use std::error::Error as StdError;
use std::net::SocketAddr;
use std::str::{from_utf8, Utf8Error};
use std::str::from_utf8;
use gitlab::webhooks::WebHook;
use hyper::{
@ -27,49 +30,6 @@ use hyper::{
};
use log::{debug, error};
#[derive(Debug)]
enum Error {
MethodMismatch,
InvalidToken,
InvalidContentType,
Hyper(hyper::Error),
SerdeJson(serde_json::Error),
Utf8(Utf8Error),
}
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 => write!(fmt, "the method is invalid"),
Error::InvalidToken => write!(fmt, "the token is invalid"),
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e),
Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e),
}
}
}
impl From<hyper::Error> for Error {
fn from(err: hyper::Error) -> Error {
Error::Hyper(err)
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
Error::SerdeJson(err)
}
}
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Error {
Error::Utf8(err)
}
}
fn error_res<E: std::fmt::Debug>(e: E) -> Result<Response<Body>, Infallible> {
error!("error response: {:?}", e);