2023-05-20 14:27:34 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2023-05-27 15:20:58 +00:00
|
|
|
use std::env::VarError;
|
2023-05-20 14:27:34 +00:00
|
|
|
use std::error::Error as StdError;
|
2023-05-27 15:20:58 +00:00
|
|
|
use std::io::Error as IoError;
|
2023-05-20 14:27:34 +00:00
|
|
|
use std::str::Utf8Error;
|
|
|
|
|
2024-07-10 11:19:04 +00:00
|
|
|
use hex::FromHexError;
|
|
|
|
use hmac::digest::InvalidLength as HmacInvalidLength;
|
2024-08-07 09:11:31 +00:00
|
|
|
use hyper::StatusCode;
|
2024-07-10 11:19:04 +00:00
|
|
|
|
2023-05-20 14:27:34 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) enum Error {
|
2024-08-07 08:51:31 +00:00
|
|
|
MethodMismatch(hyper::Method),
|
2024-07-10 12:18:28 +00:00
|
|
|
InvalidSecret,
|
2023-05-20 17:34:08 +00:00
|
|
|
InvalidContentType,
|
2024-07-10 11:19:04 +00:00
|
|
|
InvalidSignature,
|
|
|
|
InvalidRequest,
|
2024-08-30 22:36:05 +00:00
|
|
|
UnsupportedHookConversion,
|
2024-07-10 11:19:04 +00:00
|
|
|
Hex(FromHexError),
|
|
|
|
Hmac(HmacInvalidLength),
|
2023-05-20 17:34:08 +00:00
|
|
|
Hyper(hyper::Error),
|
2023-05-27 15:20:58 +00:00
|
|
|
Io(IoError),
|
2023-05-20 17:34:08 +00:00
|
|
|
SerdeJson(serde_json::Error),
|
2023-05-27 15:20:58 +00:00
|
|
|
Toml(toml::de::Error),
|
2023-05-20 17:34:08 +00:00
|
|
|
Utf8(Utf8Error),
|
2023-05-27 15:20:58 +00:00
|
|
|
Var(VarError),
|
2023-05-20 14:27:34 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 09:11:31 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 14:27:34 +00:00
|
|
|
impl StdError for Error {}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Error {
|
2023-05-20 17:34:08 +00:00
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-08-07 09:11:31 +00:00
|
|
|
Error::MethodMismatch(method) => {
|
|
|
|
write!(fmt, "method is invalid {method}, expected POST")
|
|
|
|
}
|
2024-07-10 12:18:28 +00:00
|
|
|
Error::InvalidSecret => write!(fmt, "the secret is invalid"),
|
2023-05-20 17:34:08 +00:00
|
|
|
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
|
2024-07-10 11:19:04 +00:00
|
|
|
Error::InvalidSignature => write!(fmt, "the signature is invalid"),
|
|
|
|
Error::InvalidRequest => write!(fmt, "the request is invalid"),
|
2024-08-30 22:36:05 +00:00
|
|
|
Error::UnsupportedHookConversion => write!(fmt, "Unable to convert hook"),
|
2024-07-10 11:19:04 +00:00
|
|
|
Error::Hex(e) => write!(fmt, "hex error: {}", e),
|
|
|
|
Error::Hmac(e) => write!(fmt, "hmac error: {}", e),
|
2023-05-20 17:34:08 +00:00
|
|
|
Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
|
2023-05-27 15:20:58 +00:00
|
|
|
Error::Io(e) => write!(fmt, "Io error: {}", e),
|
2023-05-20 17:34:08 +00:00
|
|
|
Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e),
|
2023-05-27 15:20:58 +00:00
|
|
|
Error::Toml(e) => write!(fmt, "toml deserialization error: {}", e),
|
2023-05-20 17:34:08 +00:00
|
|
|
Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e),
|
2023-05-27 15:20:58 +00:00
|
|
|
Error::Var(e) => write!(fmt, "Var error: {}", e),
|
2023-05-20 17:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-20 14:27:34 +00:00
|
|
|
}
|
|
|
|
|
2024-07-10 11:19:04 +00:00
|
|
|
impl From<FromHexError> for Error {
|
|
|
|
fn from(err: FromHexError) -> Error {
|
|
|
|
Error::Hex(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<HmacInvalidLength> for Error {
|
|
|
|
fn from(err: HmacInvalidLength) -> Error {
|
|
|
|
Error::Hmac(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 14:27:34 +00:00
|
|
|
impl From<hyper::Error> for Error {
|
2023-05-20 17:34:08 +00:00
|
|
|
fn from(err: hyper::Error) -> Error {
|
|
|
|
Error::Hyper(err)
|
|
|
|
}
|
2023-05-20 14:27:34 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 15:20:58 +00:00
|
|
|
impl From<IoError> for Error {
|
|
|
|
fn from(err: IoError) -> Error {
|
|
|
|
Error::Io(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 14:27:34 +00:00
|
|
|
impl From<serde_json::Error> for Error {
|
2023-05-20 17:34:08 +00:00
|
|
|
fn from(err: serde_json::Error) -> Error {
|
|
|
|
Error::SerdeJson(err)
|
|
|
|
}
|
2023-05-20 14:27:34 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 15:20:58 +00:00
|
|
|
impl From<toml::de::Error> for Error {
|
|
|
|
fn from(err: toml::de::Error) -> Error {
|
|
|
|
Error::Toml(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 14:27:34 +00:00
|
|
|
impl From<Utf8Error> for Error {
|
2023-05-20 17:34:08 +00:00
|
|
|
fn from(err: Utf8Error) -> Error {
|
|
|
|
Error::Utf8(err)
|
|
|
|
}
|
2023-05-20 14:27:34 +00:00
|
|
|
}
|
2023-05-27 15:20:58 +00:00
|
|
|
|
|
|
|
impl From<VarError> for Error {
|
|
|
|
fn from(err: VarError) -> Error {
|
|
|
|
Error::Var(err)
|
|
|
|
}
|
|
|
|
}
|