[][src]Struct dropshot::HttpError

pub struct HttpError {
    pub status_code: StatusCode,
    pub error_code: Option<String>,
    pub external_message: String,
    pub internal_message: String,
}

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

impl HttpError[src]

pub fn for_client_error(
    error_code: Option<String>,
    status_code: StatusCode,
    message: String
) -> Self
[src]

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.

pub fn for_internal_error(internal_message: String) -> Self[src]

Generates an HttpError for a 500 "Internal Server Error" error with the given internal_message for the internal message.

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

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

pub fn for_bad_request(error_code: Option<String>, message: String) -> Self[src]

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.

pub fn for_status(error_code: Option<String>, status_code: StatusCode) -> Self[src]

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

pub fn for_not_found(
    error_code: Option<String>,
    internal_message: String
) -> Self
[src]

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

pub fn into_response(self, request_id: &str) -> Response<Body>[src]

Generates an HTTP response for the given HttpError, using request_id for the response's request id.

Trait Implementations

impl Debug for HttpError[src]

impl From<Error> for HttpError[src]

impl From<Error> for HttpError[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SendSyncUnwindSafe for T where
    T: Send + Sync + UnwindSafe + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,