Struct dropshot::HttpError

source ·
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: StatusCode

HTTP 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: String

Error message to be sent to API client for this error

§internal_message: String

Error message recorded in the log for this error

Implementations§

source§

impl HttpError

source

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.

source

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.

source

pub fn for_unavail(error_code: Option<String>, internal_message: String) -> Self

Generates an HttpError for a 503 “Service Unavailable” error with the given internal_message for the internal message.

source

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.

source

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”).

source

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).

source

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§

source§

impl Debug for HttpError

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for HttpError

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Error for HttpError

source§

fn source(&self) -> Option<&(dyn Error + 'static)>

The lower-level source of this error, if any. Read more
1.0.0 · source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type based access to context intended for error reports. Read more
source§

impl From<Error> for HttpError

source§

fn from(error: HyperError) -> Self

Converts to this type from the input type.
source§

impl From<Error> for HttpError

source§

fn from(error: Error) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

impl<T> SendSyncUnwindSafe for T
where T: Send + Sync + UnwindSafe + ?Sized,

source§

impl<T> ServerContext for T
where T: 'static + Send + Sync,