swish_api/
error.rs

1//! # The error module
2//!
3//! Contains all the errors that can occur.
4//!
5extern crate hyper;
6extern crate serde_json;
7
8use hyper::http::uri;
9use std::fmt;
10use std::io;
11
12pub type ErrorCollection = Vec<SwishClientError>;
13
14#[derive(Debug)]
15pub enum SwishClientError {
16    Swish(RequestError),
17    Parse(String),
18    Http(hyper::Error),
19    Uri(uri::InvalidUri),
20    Io(io::Error),
21    Json(serde_json::Error),
22    ErrorCollection(ErrorCollection),
23}
24
25impl fmt::Display for SwishClientError {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        match *self {
28            SwishClientError::Swish(ref err) => write!(f, ": {}", err),
29            SwishClientError::Http(ref err) => write!(f, ": {}", err),
30            SwishClientError::Io(ref err) => write!(f, ": {}", err),
31            SwishClientError::Json(ref err) => write!(f, ": {}", err),
32            SwishClientError::Parse(ref err) => write!(f, ": {}", err),
33            SwishClientError::Uri(ref err) => write!(f, ": {}", err),
34            SwishClientError::ErrorCollection(ref err) => write!(
35                f,
36                ": {}",
37                err.iter()
38                    .fold(String::new(), |acc, curr| acc + &curr.to_string() + ", ")
39            ),
40        }
41    }
42}
43
44impl From<RequestError> for SwishClientError {
45    fn from(error: RequestError) -> SwishClientError {
46        SwishClientError::Swish(error)
47    }
48}
49
50impl From<ErrorCollection> for SwishClientError {
51    fn from(error: ErrorCollection) -> SwishClientError {
52        SwishClientError::ErrorCollection(error)
53    }
54}
55
56impl From<hyper::Error> for SwishClientError {
57    fn from(err: hyper::Error) -> SwishClientError {
58        SwishClientError::Http(err)
59    }
60}
61
62impl From<uri::InvalidUri> for SwishClientError {
63    fn from(err: uri::InvalidUri) -> SwishClientError {
64        SwishClientError::Uri(err)
65    }
66}
67
68impl From<io::Error> for SwishClientError {
69    fn from(err: io::Error) -> SwishClientError {
70        SwishClientError::Io(err)
71    }
72}
73
74impl From<serde_json::Error> for SwishClientError {
75    fn from(err: serde_json::Error) -> SwishClientError {
76        SwishClientError::Json(err)
77    }
78}
79
80#[derive(Debug, Deserialize, Clone)]
81pub enum ErrorCode {
82    /// PayeePaymentReference is invalid.
83    FF08,
84    /// Callback URL is missing or does not use Https.
85    RP03,
86    /// Payer alias is invalid.
87    BE18,
88    /// Payee alias is missing or empty.
89    RP01,
90    /// Amount value is missing or not a valid number.
91    PA02,
92    /// Amount value is too low.
93    AM06,
94    /// Amount value is too large.
95    AM02,
96    /// Invalid or missing Currency.
97    AM03,
98    /// Wrong formatted message.
99    RP02,
100    /// Another active PaymentRequest already exists for this payerAlias. Only applicable for E-Commerce.
101    RP06,
102    /// Payer not Enrolled.
103    ACMT03,
104    /// Counterpart is not activated.
105    ACMT01,
106    /// Payee not Enrolled.
107    ACMT07,
108    /// Parameter is not correct.
109    PA01,
110    /// Original Payment not found or original payment is more than than 13 months old.
111    RF02,
112}
113
114#[derive(Debug, Default, Deserialize, Clone)]
115pub struct RequestError {
116    #[serde(skip_deserializing)]
117    pub http_status: hyper::StatusCode,
118
119    #[serde(rename = "errorCode")]
120    pub code: Option<ErrorCode>,
121
122    #[serde(rename = "errorMessage")]
123    pub message: String,
124
125    #[serde(rename = "additionalInformation")]
126    pub additional_information: Option<String>,
127}
128
129impl fmt::Display for RequestError {
130    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131        write!(f, "{}", self.http_status)
132    }
133}