From 5a7691062b9a4056278631197ded145ed24eed70 Mon Sep 17 00:00:00 2001 From: Timo Ley Date: Fri, 23 Jun 2023 16:10:56 +0200 Subject: [PATCH] feat: better error handling --- src/error.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index 214615e..f0613c0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -46,7 +46,7 @@ impl IntoResponse for ServiceError { fn into_response(self) -> axum::http::Response { let res = match self { - ServiceError::Database(err) => ErrorResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Some(err.to_string())), + ServiceError::Database(err) => get_oracle_error_response(err), ServiceError::ErrorResponse(code, reason) => ErrorResponse::new(code, reason), }; let status = res.status; @@ -62,4 +62,18 @@ impl ErrorResponse { error: message.unwrap_or_else(|| reason.to_string()), } } +} + +fn get_oracle_error_response(error: sibyl::Error) -> ErrorResponse { + match error { + sibyl::Error::Interface(_) => ErrorResponse::new(StatusCode::INTERNAL_SERVER_ERROR, None), + sibyl::Error::Oracle(code, message) => { + if code == 1 || code == 2291 || code == 20111 { + ErrorResponse::new(StatusCode::CONFLICT, Some(message)) + } else { + ErrorResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Some(message)) + } + }, + sibyl::Error::JoinError(_) => ErrorResponse::new(StatusCode::INTERNAL_SERVER_ERROR, None), + } } \ No newline at end of file