taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

error.rs (7211B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2024, 2025, 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 use std::fmt::Display;
     18 
     19 use axum::{
     20     Json,
     21     extract::{path::ErrorKind, rejection::PathRejection},
     22     http::{HeaderMap, HeaderValue, StatusCode, header::IntoHeaderName},
     23     response::{IntoResponse, Response},
     24 };
     25 use taler_common::{
     26     api_common::ErrorDetail, api_params::ParamsErr, error_code::ErrorCode, types::payto::PaytoErr,
     27 };
     28 
     29 pub type ApiResult<T> = Result<T, ApiError>;
     30 
     31 #[derive(Debug)]
     32 pub struct ApiError {
     33     code: ErrorCode,
     34     hint: Option<Box<str>>,
     35     log: Option<Box<str>>,
     36     status: Option<StatusCode>,
     37     path: Option<Box<str>>,
     38     headers: Option<Box<HeaderMap>>,
     39 }
     40 
     41 impl ApiError {
     42     pub fn new(code: ErrorCode) -> Self {
     43         Self {
     44             code,
     45             hint: None,
     46             log: None,
     47             status: None,
     48             path: None,
     49             headers: None,
     50         }
     51     }
     52 
     53     pub fn with_hint(self, hint: impl Display) -> Self {
     54         Self {
     55             hint: Some(hint.to_string().into_boxed_str()),
     56             ..self
     57         }
     58     }
     59 
     60     pub fn with_log(self, log: impl Into<Box<str>>) -> Self {
     61         Self {
     62             log: Some(log.into()),
     63             ..self
     64         }
     65     }
     66 
     67     pub fn with_status(self, code: StatusCode) -> Self {
     68         Self {
     69             status: Some(code),
     70             ..self
     71         }
     72     }
     73 
     74     pub fn with_path(self, path: impl Display) -> Self {
     75         Self {
     76             path: Some(path.to_string().into_boxed_str()),
     77             ..self
     78         }
     79     }
     80 
     81     pub fn with_header(mut self, key: impl IntoHeaderName, value: HeaderValue) -> Self {
     82         let headers = self.headers.get_or_insert_default();
     83         headers.append(key, value);
     84         self
     85     }
     86 }
     87 
     88 impl From<sqlx::Error> for ApiError {
     89     fn from(value: sqlx::Error) -> Self {
     90         let (code, status) = match value {
     91             sqlx::Error::Configuration(_) => {
     92                 (ErrorCode::GENERIC_DB_SETUP_FAILED, StatusCode::BAD_GATEWAY)
     93             }
     94             sqlx::Error::Database(_) | sqlx::Error::Io(_) | sqlx::Error::Tls(_) => {
     95                 (ErrorCode::GENERIC_DB_FETCH_FAILED, StatusCode::BAD_GATEWAY)
     96             }
     97             sqlx::Error::PoolTimedOut => todo!(),
     98             _ => (
     99                 ErrorCode::BANK_UNMANAGED_EXCEPTION,
    100                 StatusCode::INTERNAL_SERVER_ERROR,
    101             ),
    102         };
    103         Self {
    104             code,
    105             hint: None,
    106             status: Some(status),
    107             log: Some(format!("db: {value}").into_boxed_str()),
    108             path: None,
    109             headers: None,
    110         }
    111     }
    112 }
    113 
    114 impl From<PaytoErr> for ApiError {
    115     fn from(value: PaytoErr) -> Self {
    116         failure(ErrorCode::GENERIC_PAYTO_URI_MALFORMED, value)
    117     }
    118 }
    119 
    120 impl From<ParamsErr> for ApiError {
    121     fn from(value: ParamsErr) -> Self {
    122         failure(ErrorCode::GENERIC_PARAMETER_MALFORMED, &value).with_path(value.param)
    123     }
    124 }
    125 
    126 impl From<serde_path_to_error::Error<serde_urlencoded::de::Error>> for ApiError {
    127     fn from(value: serde_path_to_error::Error<serde_urlencoded::de::Error>) -> Self {
    128         failure(ErrorCode::GENERIC_PARAMETER_MALFORMED, value.inner())
    129             .with_path(value.path().to_string())
    130             .with_log(value.to_string())
    131     }
    132 }
    133 
    134 impl From<serde_path_to_error::Error<serde_json::Error>> for ApiError {
    135     fn from(value: serde_path_to_error::Error<serde_json::Error>) -> Self {
    136         failure(ErrorCode::GENERIC_JSON_INVALID, value.inner())
    137             .with_path(value.path().to_string())
    138             .with_log(value.to_string())
    139     }
    140 }
    141 
    142 impl From<PathRejection> for ApiError {
    143     fn from(value: PathRejection) -> Self {
    144         match value {
    145             PathRejection::FailedToDeserializePathParams(err) => {
    146                 let kind = err.into_kind();
    147                 let err = failure(ErrorCode::GENERIC_PATH_SEGMENT_MALFORMED, &kind);
    148                 match kind {
    149                     ErrorKind::ParseErrorAtKey { key, .. }
    150                     | ErrorKind::InvalidUtf8InPathParam { key }
    151                     | ErrorKind::DeserializeError { key, .. } => err.with_path(key),
    152                     ErrorKind::ParseErrorAtIndex { index, .. } => err.with_path(index),
    153                     _ => err,
    154                 }
    155             }
    156             PathRejection::MissingPathParams(err) => {
    157                 failure(ErrorCode::BANK_UNMANAGED_EXCEPTION, err)
    158             }
    159             _ => failure(ErrorCode::BANK_UNMANAGED_EXCEPTION, value),
    160         }
    161     }
    162 }
    163 
    164 impl IntoResponse for ApiError {
    165     fn into_response(self) -> Response {
    166         let ApiError {
    167             code,
    168             hint,
    169             log,
    170             status,
    171             path,
    172             headers,
    173         } = self;
    174         let status_code = status.unwrap_or_else(|| {
    175             StatusCode::from_u16(code.status_code()).expect("Invalid status code")
    176         });
    177         let log = log.or(hint.clone());
    178 
    179         let mut resp = (
    180             status_code,
    181             Json(ErrorDetail {
    182                 code: code as u16,
    183                 hint,
    184                 detail: None,
    185                 parameter: None,
    186                 path,
    187                 offset: None,
    188                 index: None,
    189                 object: None,
    190                 currency: None,
    191                 type_expected: None,
    192                 type_actual: None,
    193                 extra: None,
    194             }),
    195         )
    196             .into_response();
    197         if let Some(headers) = headers {
    198             for (k, v) in *headers {
    199                 resp.headers_mut().append(k.unwrap(), v);
    200             }
    201         }
    202         resp.extensions_mut()
    203             .insert(LoggedError { code, info: log });
    204 
    205         resp
    206     }
    207 }
    208 
    209 #[derive(Debug, Clone)]
    210 pub struct LoggedError {
    211     pub code: ErrorCode,
    212     pub info: Option<Box<str>>,
    213 }
    214 
    215 pub fn failure_code(code: ErrorCode) -> ApiError {
    216     ApiError::new(code)
    217 }
    218 
    219 pub fn failure(code: ErrorCode, hint: impl Display) -> ApiError {
    220     ApiError::new(code).with_hint(hint)
    221 }
    222 
    223 pub fn failure_status(code: ErrorCode, hint: impl Display, status: StatusCode) -> ApiError {
    224     ApiError::new(code).with_hint(hint).with_status(status)
    225 }
    226 
    227 pub fn not_implemented() -> ApiError {
    228     ApiError::new(ErrorCode::END).with_status(StatusCode::NOT_IMPLEMENTED)
    229 }
    230 
    231 pub fn unauthorized() -> ApiError {
    232     ApiError::new(ErrorCode::GENERIC_UNAUTHORIZED)
    233 }
    234 
    235 pub fn forbidden(hint: impl Display) -> ApiError {
    236     ApiError::new(ErrorCode::GENERIC_FORBIDDEN).with_hint(hint)
    237 }
    238 
    239 pub fn bad_request(hint: impl Display) -> ApiError {
    240     ApiError::new(ErrorCode::GENERIC_JSON_INVALID).with_hint(hint)
    241 }