objectstore-types 0.3.2

Shared types for the Objectstore client and server
Documentation
//! Error body of the HTTP API.

use std::error::Error;

use serde::{Deserialize, Serialize};

/// A JSON error response returned by the API.
///
/// The batch endpoint embeds the same body in the `error` field of failed results.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ApiErrorResponse {
    /// The main error message.
    #[serde(default)]
    pub detail: Option<String>,
    /// Chain of error causes.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub causes: Vec<String>,
}

impl ApiErrorResponse {
    /// Creates an error response from an error, extracting the full cause chain.
    pub fn from_error<E: Error + ?Sized>(error: &E) -> Self {
        let detail = Some(error.to_string());

        let mut causes = Vec::new();
        let mut source = error.source();
        while let Some(s) = source {
            causes.push(s.to_string());
            source = s.source();
        }

        Self { detail, causes }
    }
}