Refactor errors
This commit is contained in:
parent
dae8a9cc89
commit
6c2c363561
1 changed files with 20 additions and 18 deletions
|
|
@ -4,7 +4,7 @@ use axum::{
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
Json,
|
Json,
|
||||||
};
|
};
|
||||||
use log::{info, warn};
|
use log::{error, info};
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
@ -33,12 +33,12 @@ pub enum SyncServerError {
|
||||||
impl SyncServerError {
|
impl SyncServerError {
|
||||||
pub fn serialize(&self) -> SerializedError {
|
pub fn serialize(&self) -> SerializedError {
|
||||||
match self {
|
match self {
|
||||||
Self::InitError(error) => format_anyhow_error(error),
|
Self::InitError(error) => error.into(),
|
||||||
Self::ClientError(error) => format_anyhow_error(error),
|
Self::ClientError(error) => error.into(),
|
||||||
Self::ServerError(error) => format_anyhow_error(error),
|
Self::ServerError(error) => error.into(),
|
||||||
Self::NotFound(error) => format_anyhow_error(error),
|
Self::NotFound(error) => error.into(),
|
||||||
Self::Unauthorized(error) => format_anyhow_error(error),
|
Self::Unauthorized(error) => error.into(),
|
||||||
Self::PermissionDeniedError(error) => format_anyhow_error(error),
|
Self::PermissionDeniedError(error) => error.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -64,17 +64,19 @@ pub struct SerializedError {
|
||||||
pub causes: Vec<String>,
|
pub causes: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_anyhow_error(error: &anyhow::Error) -> SerializedError {
|
impl From<&anyhow::Error> for SerializedError {
|
||||||
let mut causes = vec![];
|
fn from(error: &anyhow::Error) -> SerializedError {
|
||||||
let mut current_error = error.source();
|
let mut causes = vec![];
|
||||||
while let Some(error) = current_error {
|
let mut current_error = error.source();
|
||||||
causes.push(error.to_string());
|
while let Some(error) = current_error {
|
||||||
current_error = error.source();
|
causes.push(error.to_string());
|
||||||
}
|
current_error = error.source();
|
||||||
|
}
|
||||||
|
|
||||||
SerializedError {
|
SerializedError {
|
||||||
message: error.to_string(),
|
message: error.to_string(),
|
||||||
causes,
|
causes,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,7 +89,7 @@ pub const fn init_error(error: anyhow::Error) -> SyncServerError {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn server_error(error: anyhow::Error) -> SyncServerError {
|
pub fn server_error(error: anyhow::Error) -> SyncServerError {
|
||||||
warn!("Server error: {:?}", error);
|
error!("Server error: {:?}", error);
|
||||||
SyncServerError::ServerError(error)
|
SyncServerError::ServerError(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue