paypal_rust/client/
error.rs

1use crate::resources::link_description::LinkDescription;
2use reqwest_middleware;
3use reqwest_middleware::Error;
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6use std::borrow::Cow;
7use std::fmt::Display;
8use thiserror::Error as ThisErr;
9
10#[skip_serializing_none]
11#[derive(Clone, Debug, Default, Deserialize, Serialize)]
12pub struct ErrorDetails {
13    pub field: Option<String>,
14    pub value: Option<String>,
15    pub location: Option<String>,
16    pub issue: Option<String>,
17    pub description: Option<String>,
18}
19
20#[derive(Debug, Serialize, Deserialize, ThisErr)]
21pub struct ValidationError {
22    pub name: String,
23    pub message: String,
24    pub debug_id: Option<String>,
25    pub details: Option<Vec<ErrorDetails>>,
26    pub links: Vec<LinkDescription>,
27}
28
29impl Display for ValidationError {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        write!(
32            f,
33            "ValidationError: {} - {} - {:?} - {:?}\n Links: {:?}",
34            self.name, self.message, self.debug_id, self.details, self.links
35        )
36    }
37}
38
39pub trait ErrorResponse {
40    fn error(&self) -> Cow<str>;
41    fn error_description(&self) -> Cow<str>;
42}
43
44#[derive(Debug, ThisErr)]
45pub enum PayPalError {
46    Http(reqwest::Error),
47    Json(serde_json::Error),
48    Api(ValidationError),
49    QueryString(serde_urlencoded::ser::Error),
50    MissingAccessToken,
51    LibraryError(String),
52}
53
54impl Display for PayPalError {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        match self {
57            Self::Http(e) => write!(f, "HTTP error: {e}"),
58            Self::Json(e) => write!(f, "Failed to serialize response body: {e}"),
59            Self::Api(e) => write!(f, "API error: {e}"),
60            Self::QueryString(e) => write!(f, "Failed to serialize query string: {e}"),
61            Self::MissingAccessToken => write!(f, "Missing access token"),
62            Self::LibraryError(e) => write!(f, "Library error: {e}"),
63        }
64    }
65}
66
67impl From<reqwest::Error> for PayPalError {
68    fn from(error: reqwest::Error) -> Self {
69        Self::Http(error)
70    }
71}
72
73impl From<serde_json::Error> for PayPalError {
74    fn from(error: serde_json::Error) -> Self {
75        Self::Json(error)
76    }
77}
78
79impl From<ValidationError> for PayPalError {
80    fn from(error: ValidationError) -> Self {
81        Self::Api(error)
82    }
83}
84
85impl From<serde_urlencoded::ser::Error> for PayPalError {
86    fn from(error: serde_urlencoded::ser::Error) -> Self {
87        Self::QueryString(error)
88    }
89}
90
91impl From<Error> for PayPalError {
92    fn from(error: Error) -> Self {
93        match error {
94            Error::Reqwest(error) => Self::Http(error),
95            Error::Middleware(_) => Self::LibraryError(
96                "Middleware error should not be returned from PayPal API, please report this issue"
97                    .to_string(),
98            ),
99        }
100    }
101}