Struct headless_lms_server::domain::error::ControllerError
source · pub struct ControllerError {
error_type: <ControllerError as BackendError>::ErrorType,
message: String,
source: Option<Error>,
span_trace: SpanTrace,
backtrace: Backtrace,
}
Expand description
Represents error messages that are sent in responses. Used as the error type in ControllerError, which is used by all the controllers in the application.
All the information in the error is meant to be seen by the user. The type of error is determined by the ControllerErrorType enum, which is stored inside this struct. The type of the error determines which HTTP status code will be sent to the user.
Examples
Usage without source error
if erroneous_condition {
return Err(ControllerError::new(
ControllerErrorType::BadRequest,
"Cannot create a new account when signed in.".to_string(),
None,
));
}
Usage with a source error
Used when calling a function that returns an error that cannot be automatically converted to an ControllerError. (See impl From<X>
implementations on this struct.)
some_function_returning_an_error().map_err(|original_error| {
ControllerError::new(
ControllerErrorType::InternalServerError,
"Could not read file".to_string(),
Some(original_error.into()),
)
})?;
Example HTTP response from an error
{
"title": "Internal Server Error",
"message": "pool timed out while waiting for an open connection",
"source": "source of error"
}
Fields§
§error_type: <ControllerError as BackendError>::ErrorType
§message: String
§source: Option<Error>
Original error that caused this error.
span_trace: SpanTrace
A trace of tokio tracing spans, generated automatically when the error is generated.
backtrace: Backtrace
Stack trace, generated automatically when the error is created.
Trait Implementations§
source§impl BackendError for ControllerError
impl BackendError for ControllerError
type ErrorType = ControllerErrorType
fn new( error_type: Self::ErrorType, message: String, source_error: Option<Error> ) -> Self
fn backtrace(&self) -> Option<&Backtrace>
fn error_type(&self) -> &Self::ErrorType
fn message(&self) -> &str
fn span_trace(&self) -> &SpanTrace
fn new_with_traces( error_type: Self::ErrorType, message: String, source_error: Option<Error>, backtrace: Backtrace, span_trace: SpanTrace ) -> Self
fn to_different_error<T>( self, new_error_type: <T as BackendError>::ErrorType, new_message: String ) -> T
source§impl Debug for ControllerError
impl Debug for ControllerError
Custom formatter so that errors that get printed to the console are easy-to-read with proper context where the error is coming from.