shared/error/
billing.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
93
94
95
96
97
98
99
100
use crate::domain::billing::{AccountType, PlanType, SubscriptionType};
use crate::error::service::ServiceError;
use crate::error::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 BillingError {
    #[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("Not found: {0}")]
    NotFound(String),
    #[error("Missing Stripe signature")]
    MissingStripeSignature,
    #[error("Invalid Setup Intent ID")]
    InvalidSetupIntentId,
    #[error("No active subscription for user")]
    NoActiveSubscription,
    #[error("The current subscription is not paused")]
    SubscriptionNotPaused,
    #[error("No canceled subscription for user")]
    NoCanceledSubscription,
    #[error("Account has an existing subscription")]
    SubscriptionExists,
    #[error("School not found")]
    SchoolNotFound,
    #[error("Incorrect plan type. Expected {expected}, found {found}.")]
    IncorrectPlanType {
        expected: AccountType,
        found: SubscriptionType,
    },
    #[error("Invalid promotion code {0}")]
    InvalidPromotionCode(String),
    #[error("Forbidden")]
    Forbidden,
    #[error("Cannot upgrade to {upgrade_to} from {upgrade_from}")]
    InvalidUpgradePlanType {
        upgrade_to: PlanType,
        upgrade_from: PlanType,
    },
}

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

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

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

#[cfg(feature = "backend")]
impl ResponseError for BillingError {
    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(_) | Self::SchoolNotFound => 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)
    }
}