add new files

This commit is contained in:
Patrick Michl 2022-04-25 18:18:28 +02:00
parent 8e80f02cbc
commit a789a8cc56
No known key found for this signature in database
GPG Key ID: BFE0ACEE21CD5EB0
3 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,53 @@
use axum::http::StatusCode;
use axum::response::IntoResponse;
use super::registration_error::RegistrationError;
macro_rules! map_err {
($err:ident, $($type:path => $target:path),+) => {
$(
if $err.is::<$type>() {
return $target($err.downcast().unwrap());
}
)*
}
}
#[derive(Debug, thiserror::Error)]
pub enum ApiError {
#[error("Registration Error")]
RegistrationError(#[from] RegistrationError),
#[error("Database Error")]
DBError(#[from] sqlx::Error),
#[error("Generic Error")]
Generic(anyhow::Error),
}
impl From<anyhow::Error> for ApiError {
fn from(err: anyhow::Error) -> Self {
map_err!(err,
sqlx::Error => ApiError::DBError,
RegistrationError => ApiError::RegistrationError
);
ApiError::Generic(err)
}
}
impl IntoResponse for ApiError {
fn into_response(self) -> axum::response::Response {
match self {
ApiError::RegistrationError(registration_error) => match registration_error {
RegistrationError::InvalidUserId => {
(StatusCode::OK, String::new()).into_response()
}
RegistrationError::MissingUserId => {
(StatusCode::OK, String::new()).into_response()
}
},
_ => StatusCode::INTERNAL_SERVER_ERROR.into_response()
}
}
}

View File

@ -0,0 +1,2 @@
pub mod api_error;
pub mod registration_error;

View File

@ -0,0 +1,7 @@
#[derive(Debug, thiserror::Error)]
pub enum RegistrationError {
#[error("UserId is missing")]
MissingUserId,
#[error("UserId is invalid")]
InvalidUserId,
}