Skip to main content

objectstore_types/
api.rs

1//! Error body of the HTTP API.
2
3use std::error::Error;
4
5use serde::{Deserialize, Serialize};
6
7/// A JSON error response returned by the API.
8///
9/// The batch endpoint embeds the same body in the `error` field of failed results.
10#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
11#[non_exhaustive]
12pub struct ApiErrorResponse {
13    /// The main error message.
14    #[serde(default)]
15    pub detail: Option<String>,
16    /// Chain of error causes.
17    #[serde(default, skip_serializing_if = "Vec::is_empty")]
18    pub causes: Vec<String>,
19}
20
21impl ApiErrorResponse {
22    /// Creates an error response from an error, extracting the full cause chain.
23    pub fn from_error<E: Error + ?Sized>(error: &E) -> Self {
24        let detail = Some(error.to_string());
25
26        let mut causes = Vec::new();
27        let mut source = error.source();
28        while let Some(s) = source {
29            causes.push(s.to_string());
30            source = s.source();
31        }
32
33        Self { detail, causes }
34    }
35}