Skip to main content

ErrorExt

Trait ErrorExt 

Source
pub trait ErrorExt {
    // Required methods
    fn to_json(&self, config: FormatConfig) -> Result<Value, Error>;
    fn to_html(&self, config: FormatConfig) -> String;
    fn to_graphql(&self, config: FormatConfig) -> Result<Value, Error>;
    fn to_text(&self, config: FormatConfig) -> String;
    fn to_jsonrpc(&self, config: FormatConfig) -> Result<Value, Error>;
    fn http_status(&self) -> StatusCode;
    fn http_headers(&self) -> Vec<(HeaderName, HeaderValue)>;

    // Provided method
    fn to_debug(&self) -> String
       where Self: Debug { ... }
}
Expand description

Extension trait for formatting any error type

This trait is implemented for dyn std::error::Error and uses the error registry to dynamically dispatch to the concrete error type’s apollo_errors::Error implementation.

§Example

use apollo_errors::{ErrorExt, FormatConfig};

fn handle_error(error: &dyn std::error::Error) {
    let config = FormatConfig::default();
    let graphql = error.to_graphql(config);
    let json = error.to_json(config);
}

Required Methods§

Source

fn to_json(&self, config: FormatConfig) -> Result<Value, Error>

Render this error as JSON format

§Errors

Returns an error if any field fails to serialize to JSON

Source

fn to_html(&self, config: FormatConfig) -> String

Render this error as HTML

Source

fn to_graphql(&self, config: FormatConfig) -> Result<Value, Error>

Render this error as GraphQL JSON format

§Errors

Returns an error if any field fails to serialize to JSON

Source

fn to_text(&self, config: FormatConfig) -> String

Render this error as plain text

Source

fn to_jsonrpc(&self, config: FormatConfig) -> Result<Value, Error>

Render this error as JSON-RPC 2.0 error format

§Errors

Returns an error if any field fails to serialize to JSON

Source

fn http_status(&self) -> StatusCode

Get the HTTP status code for this error

Source

fn http_headers(&self) -> Vec<(HeaderName, HeaderValue)>

Get HTTP headers for this error

Provided Methods§

Source

fn to_debug(&self) -> String
where Self: Debug,

Render this error in debug format using Rust’s standard Debug output.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<E: Error + 'static> ErrorExt for E

Blanket implementation for any concrete error type