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§
Sourcefn to_json(&self, config: FormatConfig) -> Result<Value, Error>
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 codemessage: The error message- Additional fields from
#[extension]fields
§Errors
Returns an error if any field fails to serialize to JSON
Sourcefn to_html(&self, config: FormatConfig) -> String
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.
Sourcefn to_graphql(&self, config: FormatConfig) -> Result<Value, Error>
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 messageextensions: Additional structured data from#[extension]fieldscode: The diagnostic code (e.g., APOLLO_PRODUCT_COMPONENT_ERROR)
§Errors
Returns an error if any field fails to serialize to JSON
Sourcefn to_text(&self, config: FormatConfig) -> String
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.
Sourcefn to_jsonrpc(&self, config: FormatConfig) -> Result<Value, Error>
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 messagedata: Object containingdiagnostic_codeand any#[extension]fields
§Errors
Returns an error if any field fails to serialize to JSON
Sourcefn http_status(&self) -> StatusCode
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.
Sourcefn http_headers(&self) -> Vec<(HeaderName, HeaderValue)>
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§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".