Compare commits
4 Commits
1cf1bfcfbc
...
3640039168
| Author | SHA1 | Date | |
|---|---|---|---|
|
3640039168
|
|||
|
9fd716c752
|
|||
|
f159e12400
|
|||
|
d16a66a2f5
|
@@ -220,6 +220,27 @@ impl fmt::Display for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ServerFnError> for Error {
|
||||
#[track_caller]
|
||||
fn from(err: ServerFnError) -> Error {
|
||||
Error::new_here(ErrorType::ServerFnError(err))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
impl dioxus_fullstack::AsStatusCode for Error {
|
||||
fn as_status_code(&self) -> StatusCode {
|
||||
match &self.source {
|
||||
ErrorType::Database(msg) if *msg == (diesel::result::Error::NotFound).to_string() => {
|
||||
StatusCode::NOT_FOUND
|
||||
}
|
||||
ErrorType::Database(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorType::Error(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorType::ServerFnError(e) => e.as_status_code(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Contextualize<R> {
|
||||
/// Add context to the `Result` if it is an `Err`.
|
||||
#[track_caller]
|
||||
@@ -229,7 +250,12 @@ pub trait Contextualize<R> {
|
||||
impl<T, E: Into<Error>> Contextualize<Result<T, Error>> for Result<T, E> {
|
||||
#[track_caller]
|
||||
fn err_context(self, context: impl Into<String>) -> Result<T, Error> {
|
||||
self.map_err(|e| e.into().with_context(context))
|
||||
// Closures can't (currently) `track_caller`, so a simple map_err doesn't work
|
||||
// See https://github.com/rust-lang/rust/issues/87417
|
||||
match self {
|
||||
Ok(e) => Ok(e),
|
||||
Err(e) => Err(e.into().with_context(context)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,10 +266,10 @@ impl<T> Contextualize<Result<T, Error>> for Option<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Into<Error>> Contextualize<Error> for E {
|
||||
impl<T, E: Into<Error>> Contextualize<Result<T, Error>> for E {
|
||||
#[track_caller]
|
||||
fn err_context(self, context: impl Into<String>) -> Error {
|
||||
self.into().with_context(context)
|
||||
fn err_context(self, context: impl Into<String>) -> Result<T, Error> {
|
||||
Err(self.into().with_context(context))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,6 +282,9 @@ pub enum ErrorType {
|
||||
|
||||
#[error("{0}")]
|
||||
Error(String),
|
||||
|
||||
#[error("Server function error: {0}")]
|
||||
ServerFnError(ServerFnError),
|
||||
}
|
||||
|
||||
impl From<ErrorType> for Error {
|
||||
|
||||
Reference in New Issue
Block a user