pub struct HttpError {
pub status_code: StatusCode,
pub error_code: Option<String>,
pub external_message: String,
pub internal_message: String,
}Expand description
HttpError represents an error generated as part of handling an API
request. When these bubble up to the top of the request handling stack
(which is most of the time that they’re generated), these are turned into an
HTTP response, which includes:
- a status code, which is likely either 400-level (indicating a client error, like bad input) or 500-level (indicating a server error).
- a structured (JSON) body, which includes:
- a string error code, which identifies the underlying error condition so that clients can potentially make programmatic decisions based on the error type
- a string error message, which is the human-readable summary of the issue, intended to make sense for API users (i.e., not API server developers)
- optionally: additional metadata describing the issue. For a validation error, this could include information about which parameter was invalid and why. This should conform to a schema associated with the error code.
It’s easy to go overboard with the error codes and metadata. Generally, we should avoid creating specific codes and metadata unless there’s a good reason for a client to care.
Besides that, HttpErrors also have an internal error message, which may
differ from the error message that gets reported to users. For example, if
the request fails because an internal database is unreachable, the client may
just see “internal error”, while the server log would include more details
like “failed to acquire connection to database at 10.1.2.3”.
Fields
status_code: StatusCodeHTTP status code for this error
error_code: Option<String>Optional string error code for this error. Callers are advised to use an enum to populate this field.
external_message: StringError message to be sent to API client for this error
internal_message: StringError message recorded in the log for this error
Implementations
sourceimpl HttpError
impl HttpError
sourcepub fn for_client_error(
error_code: Option<String>,
status_code: StatusCode,
message: String
) -> Self
pub fn for_client_error(
error_code: Option<String>,
status_code: StatusCode,
message: String
) -> Self
Generates an HttpError for any 400-level client error with a custom
message used for both the internal and external message. The
expectation here is that for most 400-level errors, there’s no need for a
separate internal message.
sourcepub fn for_internal_error(internal_message: String) -> Self
pub fn for_internal_error(internal_message: String) -> Self
Generates an HttpError for a 500 “Internal Server Error” error with the
given internal_message for the internal message.
Generates an HttpError for a 503 “Service Unavailable” error with the
given internal_message for the internal message.
sourcepub fn for_bad_request(error_code: Option<String>, message: String) -> Self
pub fn for_bad_request(error_code: Option<String>, message: String) -> Self
Generates a 400 “Bad Request” error with the given message used for
both the internal and external message. This is a convenience wrapper
around HttpError::for_client_error.
sourcepub fn for_status(error_code: Option<String>, status_code: StatusCode) -> Self
pub fn for_status(error_code: Option<String>, status_code: StatusCode) -> Self
Generates an HttpError for the given HTTP status_code where the
internal and external messages for the error come from the standard label
for this status code (e.g., the message for status code 404 is “Not
Found”).
sourcepub fn for_not_found(
error_code: Option<String>,
internal_message: String
) -> Self
pub fn for_not_found(
error_code: Option<String>,
internal_message: String
) -> Self
Generates an HttpError for a 404 “Not Found” error with a custom
internal message internal_message. The external message will be “Not
Found” (i.e., the standard label for status code 404).
sourcepub fn into_response(self, request_id: &str) -> Response<Body>
pub fn into_response(self, request_id: &str) -> Response<Body>
Generates an HTTP response for the given HttpError, using request_id
for the response’s request id.
Trait Implementations
sourceimpl Error for HttpError
impl Error for HttpError
sourcefn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
The lower-level source of this error, if any. Read more
sourcefn backtrace(&self) -> Option<&Backtrace>
fn backtrace(&self) -> Option<&Backtrace>
backtrace)Returns a stack backtrace, if available, of where this error occurred. Read more
1.0.0 · sourcefn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Auto Trait Implementations
impl RefUnwindSafe for HttpError
impl Send for HttpError
impl Sync for HttpError
impl Unpin for HttpError
impl UnwindSafe for HttpError
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber to this type, returning a
WithDispatch wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more