Add http errors
This commit is contained in:
parent
c336f84af9
commit
d0f45233b1
1 changed files with 57 additions and 0 deletions
57
backend/sync_server/src/errors.rs
Normal file
57
backend/sync_server/src/errors.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum SyncServerError {
|
||||
#[error("Initialisation error: {0}")]
|
||||
InitError(#[source] anyhow::Error),
|
||||
|
||||
#[error("Client error: {0}")]
|
||||
ClientError(#[source] anyhow::Error),
|
||||
|
||||
#[error("Server error: {0}")]
|
||||
ServerError(#[source] anyhow::Error),
|
||||
|
||||
#[error("Not found: {0}")]
|
||||
NotFound(#[source] anyhow::Error),
|
||||
|
||||
#[error("Permission denier error: {0}")]
|
||||
PermissionDeniedError(#[source] anyhow::Error),
|
||||
}
|
||||
|
||||
impl IntoResponse for SyncServerError {
|
||||
fn into_response(self) -> Response {
|
||||
let body = self.to_string();
|
||||
|
||||
match self {
|
||||
Self::InitError(_) => (StatusCode::INTERNAL_SERVER_ERROR, body).into_response(),
|
||||
Self::ClientError(_) => (StatusCode::BAD_REQUEST, body).into_response(),
|
||||
Self::ServerError(_) => (StatusCode::INTERNAL_SERVER_ERROR, body).into_response(),
|
||||
Self::NotFound(_) => (StatusCode::NOT_FOUND, body).into_response(),
|
||||
Self::PermissionDeniedError(_) => (StatusCode::FORBIDDEN, body).into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_error(error: anyhow::Error) -> SyncServerError {
|
||||
SyncServerError::InitError(error)
|
||||
}
|
||||
|
||||
pub fn server_error(error: anyhow::Error) -> SyncServerError {
|
||||
SyncServerError::ServerError(error)
|
||||
}
|
||||
|
||||
pub fn client_error(error: anyhow::Error) -> SyncServerError {
|
||||
SyncServerError::ClientError(error)
|
||||
}
|
||||
|
||||
pub fn not_found_error(error: anyhow::Error) -> SyncServerError {
|
||||
SyncServerError::NotFound(error)
|
||||
}
|
||||
|
||||
pub fn permission_denied_error(error: anyhow::Error) -> SyncServerError {
|
||||
SyncServerError::PermissionDeniedError(error)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue