1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use super::status::Status;
use std::error::Error;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JsonApiErrorArray {
    pub errors: Vec<JsonApiError>,
}

impl JsonApiErrorArray {
    pub fn new<T: Error>(error: &T, status: Status) -> JsonApiErrorArray {
        JsonApiErrorArray { errors: vec![JsonApiError::new(error, status)] }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JsonApiError {
    pub title: String,
    pub status: String,
    pub detail: String,
}

impl JsonApiError {
    pub fn new<T: Error>(error: &T, status: Status) -> JsonApiError {
        JsonApiError {
            title: error.description().to_string(),
            status: status.to_u16().to_string(),
            detail: format!("{}", error),
        }
    }
}