hyperlane-plugin-problem-details 23.3.0

An RFC 9457 Problem Details (application/problem+json) error normalizer and builder plugin for the Hyperlane framework with zero-overhead 2xx/3xx fast path and automatic 4xx/5xx error structuring.
Documentation
use super::*;

/// Structured RFC 9457 Problem Details payload.
#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ProblemDetails {
    /// URI reference identifying the problem type (`"about:blank"` by default).
    #[serde(rename = "type")]
    pub(crate) problem_type: String,
    /// Short, human-readable summary of the problem type.
    pub(crate) title: String,
    /// HTTP status code generated by the origin server.
    #[get(type(copy))]
    pub(crate) status: usize,
    /// Optional human-readable explanation specific to this occurrence.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) detail: Option<String>,
    /// Optional URI reference identifying this specific occurrence (typically the request path).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) instance: Option<String>,
    /// Optional custom extension members flattened into the top-level JSON object.
    #[serde(flatten, default, skip_serializing_if = "BTreeMap::is_empty")]
    pub(crate) extensions: BTreeMap<String, Value>,
}

/// Configuration for [`ProblemDetailsPlugin`].
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct ProblemDetailsConfig {
    /// Automatically convert empty or plain-text `4xx`/`5xx` responses into `application/problem+json`.
    #[get(type(copy))]
    pub(crate) auto_normalize: bool,
    /// Include the incoming request path as the `instance` field during auto-normalization.
    #[get(type(copy))]
    pub(crate) include_instance: bool,
    /// Optional base URI prefix for generated problem types (for example `"https://api.example.com/errors"`).
    pub(crate) type_base_uri: Option<String>,
    /// Redact raw `5xx` plain-text bodies instead of exposing internal details in `detail`.
    #[get(type(copy))]
    pub(crate) mask_server_error_details: bool,
}

/// Compiled RFC 9457 response normalizer backing [`ProblemDetailsHook`].
#[derive(Clone, Debug)]
pub struct ProblemDetailsPlugin {
    /// Retained configuration.
    pub(crate) config: ProblemDetailsConfig,
    /// Trimmed base URI without trailing slash, if configured.
    pub(crate) type_base_clean: Option<String>,
}

/// Opt-in response middleware normalizing `4xx`/`5xx` error responses into RFC 9457 Problem Details.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::response_middleware(-62)]
pub struct ProblemDetailsHook;