shared/error/
account.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate::domain::billing::{SchoolNameId, SchoolNameValue};
use crate::error::billing::BillingError;
use crate::error::{ServiceError, TransientError};
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[cfg(feature = "backend")]
use actix_web::{body::BoxBody, HttpResponse, ResponseError};
#[cfg(feature = "backend")]
use stripe::StripeError;

#[allow(missing_docs)]
#[derive(Debug, Error, Serialize, Deserialize)]
pub enum AccountError {
    #[cfg_attr(feature = "backend", error(transparent))]
    #[cfg_attr(not(feature = "backend"), error("Internal server error"))]
    InternalServerError(
        #[serde(skip)]
        #[from]
        TransientError<anyhow::Error>,
    ),
    #[error(transparent)]
    Service(ServiceError),
    #[cfg_attr(feature = "backend", error(transparent))]
    #[cfg_attr(not(feature = "backend"), error("Stripe error"))]
    Stripe(
        #[cfg(feature = "backend")]
        #[serde(skip)]
        #[from]
        TransientError<StripeError>,
    ),
    #[error("User already has an existing account")]
    UserHasAccount,
    #[error("A school name of {0} already exists")]
    SchoolNameExists(SchoolNameValue),
    #[error("A school using a name with ID {0} already exists")]
    SchoolExists(SchoolNameId),
    #[error("{0}")]
    NotFound(String),
    #[error("Forbidden")]
    Forbidden,
    #[error("{0}")]
    BadRequest(String),
}

#[cfg(feature = "backend")]
impl From<StripeError> for AccountError {
    fn from(value: StripeError) -> Self {
        Self::from(TransientError::from(value))
    }
}

impl From<ServiceError> for AccountError {
    fn from(err: ServiceError) -> Self {
        Self::Service(err)
    }
}

impl From<AccountError> for BillingError {
    fn from(value: AccountError) -> Self {
        match value {
            AccountError::Forbidden => Self::Forbidden,
            AccountError::InternalServerError(error) => Self::InternalServerError(error),
            error => TransientError::from(anyhow::Error::from(error)).into(),
        }
    }
}

impl From<anyhow::Error> for AccountError {
    fn from(e: anyhow::Error) -> Self {
        Self::InternalServerError(e.into())
    }
}

#[cfg(feature = "backend")]
impl ResponseError for AccountError {
    fn status_code(&self) -> http::StatusCode {
        match self {
            Self::InternalServerError { .. } | Self::Stripe { .. } => {
                http::StatusCode::INTERNAL_SERVER_ERROR
            }
            Self::Service(service) => service.status_code(),
            Self::NotFound(_) => http::StatusCode::NOT_FOUND,
            Self::Forbidden => http::StatusCode::FORBIDDEN,
            _ => http::StatusCode::BAD_REQUEST,
        }
    }

    fn error_response(&self) -> HttpResponse<BoxBody> {
        HttpResponse::build(self.status_code()).json(self)
    }
}