use std::error::Error;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ApiErrorResponse {
#[serde(default)]
pub detail: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub causes: Vec<String>,
}
impl ApiErrorResponse {
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 }
}
}