Skip to main content

Error

Trait Error 

Source
pub trait Error: Error + Diagnostic {
    // 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

The core trait for apollo errors.

This trait extends std::error::Error and miette::Diagnostic to provide multi-format rendering.

§Derive Macro

Use #[derive(apollo_errors::Error)] to automatically implement this trait. The derive macro generates format renderers for JSON, GraphQL, HTML, and text output.

§Example

use apollo_errors::Error;

#[derive(Debug, Error)]
pub enum MyError {
    #[error("Something went wrong")]
    #[diagnostic(code(my_product::component::error_name))]
    SomethingWrong {
        #[extension]
        field: String,
    },
}

Required Methods§

Source

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

Render this error as JSON format

config controls field name casing and error code format in the output.

Returns a serde_json::Value with:

  • error: The error code
  • message: The error message
  • Additional fields from #[extension] fields
§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

config controls field name casing and error code format in the output. Returns an HTML representation suitable for browser display.

Source

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

Render this error as GraphQL JSON format

config controls field name casing and error code format in the output.

Returns a serde_json::Value with:

  • message: The error message
  • extensions: Additional structured data from #[extension] fields
  • code: The diagnostic code (e.g., APOLLO_PRODUCT_COMPONENT_ERROR)
§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

config controls field name casing and error code format in the output. Returns a human-readable plain text representation.

Source

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

Render this error as JSON-RPC 2.0 error format

config controls field name casing and error code format in the output.

Returns a serde_json::Value with:

  • code: Integer error code (from #[jsonrpc_code(...)] or default -32000)
  • message: The error message
  • data: Object containing diagnostic_code and any #[extension] fields
§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

Returns the appropriate HTTP status code (e.g., 400, 404, 500). Defaults to 500 if not specified.

Source

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

Get HTTP headers that should be returned with this error

Returns header name-value pairs from fields marked with #[http_header("...")]. For Option fields, headers are only included if the value is Some.

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§