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§
Sourcefn to_json(&self) -> Result<Value, Error>
fn to_json(&self) -> Result<Value, Error>
Render this error as JSON format
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_debug(&self) -> String
fn to_debug(&self) -> String
Render this error in debug format
Returns a detailed debug representation
Sourcefn to_html(&self) -> String
fn to_html(&self) -> String
Render this error as HTML
Returns an HTML representation suitable for browser display
Sourcefn to_graphql(&self) -> Result<Value, Error>
fn to_graphql(&self) -> Result<Value, Error>
Render this error as GraphQL JSON format
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) -> String
fn to_text(&self) -> String
Render this error as plain text
Returns a human-readable plain text representation
Sourcefn to_jsonrpc(&self) -> Result<Value, Error>
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 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.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".