1use std::error::Error;
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
11#[non_exhaustive]
12pub struct ApiErrorResponse {
13 #[serde(default)]
15 pub detail: Option<String>,
16 #[serde(default, skip_serializing_if = "Vec::is_empty")]
18 pub causes: Vec<String>,
19}
20
21impl ApiErrorResponse {
22 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}