33 lines
1.2 KiB
Rust
33 lines
1.2 KiB
Rust
use crate::modules::api::model::api_outcome::{ApiError, ApiErrorWrapper};
|
|
use rocket::serde::json::Json;
|
|
|
|
/// Transforms diesel error into ApiErrorWrapper with proper error message.
|
|
/// Currently checks for NotFound and UniquieViolation
|
|
pub fn translate_diesel(error: diesel::result::Error) -> Json<ApiErrorWrapper> {
|
|
match error {
|
|
diesel::result::Error::DatabaseError(kind, _) => match kind {
|
|
diesel::result::DatabaseErrorKind::UniqueViolation => Json(
|
|
ApiError::new(
|
|
409,
|
|
"Es existiert bereits eine ähnliche Ressource!".to_string(),
|
|
)
|
|
.to_wrapper(),
|
|
),
|
|
_ => Json(
|
|
ApiError::new(500, "Es ist ein Datenbankfehler aufgetreten.".to_string())
|
|
.to_wrapper(),
|
|
),
|
|
},
|
|
diesel::result::Error::NotFound => Json(
|
|
ApiError::new(
|
|
404,
|
|
"Die angeforderte Ressource konnte nicht gefunden werden.".to_string(),
|
|
)
|
|
.to_wrapper(),
|
|
),
|
|
_ => Json(
|
|
ApiError::new(500, "Es ist ein Datenbankfehler aufgetreten.".to_string()).to_wrapper(),
|
|
),
|
|
}
|
|
}
|