-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8dbfffa
commit a96acbd
Showing
12 changed files
with
545 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,81 @@ | ||
use axum::http::StatusCode; | ||
use axum::response::{IntoResponse, Response}; | ||
|
||
pub type Result<T> = core::result::Result<T, (StatusCode, String)>; | ||
#[derive(Debug)] | ||
pub enum ExplorerError { | ||
Custom(String), | ||
DBError(sqlx::Error), | ||
IOError(std::io::Error), | ||
TomlDeError(toml::de::Error), | ||
HexError(rustc_hex::FromHexError), | ||
ParseUrlError(url::ParseError), | ||
SerdeJsonError(serde_json::Error), | ||
ReqwestError(reqwest::Error), | ||
} | ||
|
||
impl From<reqwest::Error> for ExplorerError { | ||
fn from(e: reqwest::Error) -> Self { | ||
ExplorerError::ReqwestError(e) | ||
} | ||
} | ||
|
||
impl From<serde_json::Error> for ExplorerError { | ||
fn from(e: serde_json::Error) -> Self { | ||
ExplorerError::SerdeJsonError(e) | ||
} | ||
} | ||
|
||
impl From<String> for ExplorerError { | ||
fn from(e: String) -> Self { | ||
ExplorerError::Custom(e) | ||
} | ||
} | ||
|
||
impl From<url::ParseError> for ExplorerError { | ||
fn from(e: url::ParseError) -> Self { | ||
ExplorerError::ParseUrlError(e) | ||
} | ||
} | ||
|
||
pub fn internal_error<E>(err: E) -> (StatusCode, String) | ||
where | ||
E: std::error::Error, | ||
{ | ||
let err_msg = err.to_string(); | ||
if err_msg.contains("no rows") { | ||
return (StatusCode::NOT_FOUND, "not found".to_string()); | ||
impl From<rustc_hex::FromHexError> for ExplorerError { | ||
fn from(e: rustc_hex::FromHexError) -> Self { | ||
ExplorerError::HexError(e) | ||
} | ||
} | ||
|
||
impl From<std::io::Error> for ExplorerError { | ||
fn from(e: std::io::Error) -> Self { | ||
ExplorerError::IOError(e) | ||
} | ||
} | ||
|
||
impl From<toml::de::Error> for ExplorerError { | ||
fn from(e: toml::de::Error) -> Self { | ||
ExplorerError::TomlDeError(e) | ||
} | ||
} | ||
|
||
(StatusCode::INTERNAL_SERVER_ERROR, err_msg) | ||
impl From<sqlx::Error> for ExplorerError { | ||
fn from(e: sqlx::Error) -> Self { | ||
ExplorerError::DBError(e) | ||
} | ||
} | ||
|
||
pub type Result<T> = core::result::Result<T, ExplorerError>; | ||
|
||
impl IntoResponse for ExplorerError { | ||
fn into_response(self) -> Response { | ||
let err_msg = match self { | ||
ExplorerError::Custom(e) => e, | ||
ExplorerError::DBError(e) => e.to_string(), | ||
ExplorerError::IOError(e) => e.to_string(), | ||
ExplorerError::TomlDeError(e) => e.to_string(), | ||
ExplorerError::HexError(e) => e.to_string(), | ||
ExplorerError::ParseUrlError(e) => e.to_string(), | ||
ExplorerError::SerdeJsonError(e) => e.to_string(), | ||
ExplorerError::ReqwestError(e) => e.to_string(), | ||
}; | ||
|
||
(StatusCode::INTERNAL_SERVER_ERROR, err_msg).into_response() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.