Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

2.4 KB
Raw Blame
use api_models::errors::types::{ApiError, ApiErrorResponse};
use common_utils::errors::{CustomResult, ErrorSwitch};

pub type AnalyticsResult<T> = CustomResult<T, AnalyticsError>;

#[derive(Debug, Clone, serde::Serialize, thiserror::Error)]
pub enum AnalyticsError {
    #[allow(dead_code)]
    #[error("Not implemented: {0}")]
    NotImplemented(&'static str),
    #[error("Unknown Analytics Error")]
    UnknownError,
    #[error("Access Forbidden Analytics Error")]
    AccessForbiddenError,
    #[error("Failed to fetch currency exchange rate")]
    ForexFetchFailed,
    #[error("Missing email")]
    MissingEmail,
    #[error("Invalid URL scheme: {0}")]
    InvalidReturnUrl(String),
    #[error("Invalid report columns: {0}")]
    InvalidReportColumns(String),
}

impl ErrorSwitch<ApiErrorResponse> for AnalyticsError {
    fn switch(&self) -> ApiErrorResponse {
        match self {
            Self::NotImplemented(feature) => ApiErrorResponse::NotImplemented(ApiError::new(
                "IR",
                0,
                format!("{feature} is not implemented."),
                None,
            )),
            Self::UnknownError => ApiErrorResponse::InternalServerError(ApiError::new(
                "HE",
                0,
                "Something went wrong",
                None,
            )),
            Self::AccessForbiddenError => {
                ApiErrorResponse::Unauthorized(ApiError::new("IR", 0, "Access Forbidden", None))
            }
            Self::ForexFetchFailed => ApiErrorResponse::InternalServerError(ApiError::new(
                "HE",
                0,
                "Failed to fetch currency exchange rate",
                None,
            )),
            Self::MissingEmail => ApiErrorResponse::BadRequest(ApiError::new(
                "IR",
                6,
                "Missing or invalid merchant email address.",
                None,
            )),
            Self::InvalidReturnUrl(invalid_url_err) => ApiErrorResponse::BadRequest(ApiError::new(
                "IR",
                6,
                format!("Invalid return URL: {invalid_url_err}"),
                None,
            )),
            Self::InvalidReportColumns(reason) => ApiErrorResponse::BadRequest(ApiError::new(
                "IR",
                6,
                format!("Invalid report columns: {reason}"),
                None,
            )),
        }
    }
}