Skip to main content

Error

Trait Error 

Source
pub trait Error: Error + Diagnostic {
    // Required methods
    fn to_json(&self) -> Result<Value, Error>;
    fn to_debug(&self) -> String;
    fn to_html(&self) -> String;
    fn to_graphql(&self) -> Result<Value, Error>;
    fn to_text(&self) -> String;
    fn to_jsonrpc(&self) -> Result<Value, Error>;
    fn http_status(&self) -> StatusCode;
    fn http_headers(&self) -> Vec<(HeaderName, HeaderValue)>;
}
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) -> Result<Value, Error>

Render this error as JSON format

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_debug(&self) -> String

Render this error in debug format

Returns a detailed debug representation

Source

fn to_html(&self) -> String

Render this error as HTML

Returns an HTML representation suitable for browser display

Source

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

Render this error as GraphQL JSON format

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

Render this error as plain text

Returns a human-readable plain text representation

Source

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

Render this error as JSON-RPC 2.0 error format

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.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§