Skip to main content

HeapErrorExt

Trait HeapErrorExt 

Source
pub trait HeapErrorExt {
    // 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_debug(&self) -> String;
    fn to_jsonrpc(&self, config: FormatConfig) -> Result<Value, Error>;
    fn http_status(&self) -> StatusCode;
    fn http_headers(&self) -> Vec<(HeaderName, HeaderValue)>;
}
Expand description

Extension trait for heap-allocated error types without ’static bounds

This trait provides error formatting for heap-allocated errors like Box<dyn Error + Send + Sync> and Arc<dyn Error + Send + Sync> that don’t have 'static bounds. These types are commonly used in async contexts (e.g., tower::BoxError) where the error lifetime isn’t statically known.

§Why This Trait Exists

The regular ErrorExt trait requires 'static bounds because it uses the registry’s TypeId-based dispatch. However, types like Box<dyn Error + Send + Sync> cannot satisfy the 'static requirement. This trait provides an alternative path that:

  1. Unwraps the heap-allocated error to get &dyn Error
  2. Attempts nested wrapper unwrapping (e.g., Box<Arc<T>>)
  3. Tries registry lookup on the inner error
  4. Falls back to generic error rendering if no match is found

§Example

use apollo_errors::{HeapErrorExt, FormatConfig};
use tower::BoxError;

fn handle_boxed_error(error: BoxError) {
    let config = FormatConfig::default();
    let json = error.to_json(config);
    let text = error.to_text(config);
}

Required Methods§

Source

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

Render this heap-allocated 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 heap-allocated error as HTML

Source

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

Render this heap-allocated 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 heap-allocated error as plain text

Source

fn to_debug(&self) -> String

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

Source

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

Render this heap-allocated 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 heap-allocated error

Source

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

Get HTTP headers for this heap-allocated error

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl HeapErrorExt for Arc<dyn Error + Send + Sync>

Implementation for Arc<dyn Error + Send + Sync>

Similar to the Box implementation but for reference-counted errors. This is less common but can occur in scenarios where errors need to be shared across multiple tasks or threads.

Source§

impl HeapErrorExt for Box<dyn Error + Send + Sync>

Implementation for Box<dyn Error + Send + Sync>

This is the most common case for async error handling (e.g., tower::BoxError). The implementation unwraps the Box, checks for nested Arc wrappers, and attempts registry lookup before falling back to generic error formatting.

Implementors§