pinpayments/
error.rs

1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4use crate::params::to_snakecase;
5
6/// An error encountered using the Pin Payments API.
7#[derive(Debug, Error)]
8pub enum PinError {
9    #[error("error reported by Pin Payments: {0}")]
10    PinPayments(#[from] ErrorResponse),
11    #[error("error serializing or deserializing a querystring: {0}")]
12    QueryStringSerialize(#[from] serde_path_to_error::Error<serde_qs::Error>),
13    #[error("error serializing or deserializing a request")]
14    JSONSerialize(#[from] serde_path_to_error::Error<serde_json::Error>),
15    #[error("error communicating with Pin Payments: {0}")]
16    ClientError(String),
17    #[error("timeout communicating with Pin Payments")]
18    Timeout,
19}
20
21impl From<http_types::Error> for PinError {
22    fn from(err: http_types::Error) -> PinError {
23        PinError::ClientError(err.to_string())
24    }
25}
26
27/// The list of possible values for a RequestError code.
28#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq, Hash)]
29#[serde(rename_all = "snake_case")]
30#[non_exhaustive]
31pub enum ErrorCode {
32    ParameterMissing,
33    ParameterUnknown,
34}
35
36impl std::fmt::Display for ErrorCode {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        write!(f, "{}", to_snakecase(&format!("{:?}", self)))
39    }
40}
41
42#[derive(Debug, Default, Deserialize, Error)]
43#[error("{error} message: {error_description}")]
44pub struct ErrorResponse {
45    pub error: String,
46    pub error_description: String,
47}