paypal_rs/
errors.rs

1//! Errors created by this crate.
2use crate::data::common::LinkDescription;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::error::Error;
6use std::fmt;
7
8/// A paypal api response error.
9#[derive(Debug, Serialize, Deserialize)]
10pub struct PaypalError {
11    /// The error name.
12    pub name: String,
13    /// The error message.
14    pub message: Option<String>,
15    /// Paypal debug id
16    pub debug_id: Option<String>,
17    /// Error details
18    pub details: Vec<HashMap<String, String>>,
19    /// Only available on Identity errors
20    pub error: Option<String>,
21    /// Only available on Identity errors
22    pub error_description: Option<String>,
23    /// Links with more information about the error.
24    pub links: Vec<LinkDescription>,
25}
26
27impl fmt::Display for PaypalError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(f, "{:#?}", self)
30    }
31}
32
33impl Error for PaypalError {}
34
35/// A response error, it may be paypal related or an error related to the http request itself.
36#[derive(Debug)]
37pub enum ResponseError {
38    /// A paypal api error.
39    ApiError(PaypalError),
40    /// A http error.
41    HttpError(reqwest::Error),
42}
43
44impl fmt::Display for ResponseError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            ResponseError::ApiError(e) => write!(f, "{}", e),
48            ResponseError::HttpError(e) => write!(f, "{}", e),
49        }
50    }
51}
52
53impl Error for ResponseError {
54    fn source(&self) -> Option<&(dyn Error + 'static)> {
55        match self {
56            ResponseError::ApiError(e) => Some(e),
57            ResponseError::HttpError(e) => Some(e),
58        }
59    }
60}
61
62// Implemented so we can use ? directly on it.
63impl From<PaypalError> for ResponseError {
64    fn from(e: PaypalError) -> Self {
65        ResponseError::ApiError(e)
66    }
67}
68
69// Implemented so we can use ? directly on it.
70impl From<reqwest::Error> for ResponseError {
71    fn from(e: reqwest::Error) -> Self {
72        ResponseError::HttpError(e)
73    }
74}
75
76/// When a currency is invalid.
77#[derive(Debug)]
78pub struct InvalidCurrencyError(pub String);
79
80impl fmt::Display for InvalidCurrencyError {
81    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82        write!(f, "{:?} is not a valid currency", self.0)
83    }
84}
85
86impl Error for InvalidCurrencyError {}
87
88/// When a country is invalid.
89#[derive(Debug)]
90pub struct InvalidCountryError(pub String);
91
92impl fmt::Display for InvalidCountryError {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        write!(f, "{:?} is not a valid country", self.0)
95    }
96}
97
98impl Error for InvalidCountryError {}