Skip to main content

ecbdp_api/
error.rs

1// Error types
2use std::error::Error as ErrorTrait;
3use serde_json::Error as SerdeJSONError;
4use chrono::format::ParseError as ChronoParseError;
5use reqwest::Error as ReqwestError;
6// Dependencies
7use std::{fmt::Display, convert::From};
8
9
10#[derive(Debug)]
11pub enum Error {
12    WrongResourceRequested,
13    MissingQueryAttribute { attribute: String, },
14    MissingKeyAttribute { attribute: String, },
15    // Status code errors
16    SC400,
17    SC404,
18    SC406,
19    SC500,
20    SC501,
21    SC503,
22    // Serde JSON errors
23    SerdeJSONError(SerdeJSONError),
24    // Chrono errors
25    ChronoParseError(ChronoParseError),
26    // Reqwest errors
27    ReqwestError(ReqwestError),
28}
29
30impl Display for Error {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            Self::WrongResourceRequested => write!(f, "Wrong Resource Requested: The query's resource is incompatible with this operation."),
34            Self::MissingQueryAttribute { attribute } => write!(f, "Missing Query Attribute: {attribute} is missing from the query."),
35            Self::MissingKeyAttribute { attribute } => write!(f, "Missing Key Attribute: The key is missing a `{attribute}` attribute."),
36            // Status code error
37            Self::SC400 => write!(f, "Status Code 400 (Syntax Error): There is a syntactic or semantic issue with the parameters you supplied."),
38            Self::SC404 => write!(f, "Status Code 404 (No Results Found): No results matching the query."),
39            Self::SC406 => write!(f, "Status Code 406 (Not Acceptable): You ask for a resource representation that ECB Data Portal does not support."),
40            Self::SC500 => write!(f, "Status Code 500 (Internal Server Error): ECB Data Portal internal issue."),
41            Self::SC501 => write!(f, "Status Code 501 (Not Implemented): ECB Data Portal web service offers a subset of the functionality offered by the SDMX RESTful web service specification. You use a feature that has not yet been implemented."),
42            Self::SC503 => write!(f, "Status Code 503 (Service Anavailable): ECB Data Portal service is unavailable."),
43            // Serde JSON errors
44            Self::SerdeJSONError(e) => write!(f, "Serde JSON Error: {}", e.to_string()),
45            // Chrono errors
46            Self::ChronoParseError(e) => write!(f, "Chrono Parse Error: {}", e.to_string()),
47            // Reqwest errors
48            Self::ReqwestError(e) => write!(f, "Reqwest Error: {}", e.to_string()),
49        }
50    }
51}
52
53impl ErrorTrait for Error {}
54
55impl From<SerdeJSONError> for Error {
56    fn from(value: SerdeJSONError) -> Self { Self::SerdeJSONError(value) }
57}
58
59impl From<ChronoParseError> for Error {
60    fn from(value: ChronoParseError) -> Self { Self::ChronoParseError(value) }
61}
62
63impl From<ReqwestError> for Error {
64    fn from(value: ReqwestError) -> Self { Self::ReqwestError(value) }
65}