taud/
error.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use darkfi::rpc::jsonrpc::{ErrorCode, JsonError, JsonResponse, JsonResult};
20use tinyjson::JsonValue;
21
22#[derive(Debug, thiserror::Error)]
23pub enum TaudError {
24    #[error("Due timestamp invalid")]
25    InvalidDueTime,
26    #[error("Invalid Id")]
27    InvalidId,
28    #[error("Invalid Data/Params: `{0}` ")]
29    InvalidData(String),
30    #[error("InternalError")]
31    Darkfi(#[from] darkfi::error::Error),
32    #[error("Json serialization error: `{0}`")]
33    JsonError(String),
34    #[error("Encryption error: `{0}`")]
35    EncryptionError(String),
36    #[error("Decryption error: `{0}`")]
37    DecryptionError(String),
38    #[error("IO Error: `{0}`")]
39    IoError(String),
40}
41
42pub type TaudResult<T> = std::result::Result<T, TaudError>;
43
44impl From<crypto_box::aead::Error> for TaudError {
45    fn from(err: crypto_box::aead::Error) -> TaudError {
46        TaudError::EncryptionError(err.to_string())
47    }
48}
49
50impl From<std::io::Error> for TaudError {
51    fn from(err: std::io::Error) -> TaudError {
52        TaudError::IoError(err.to_string())
53    }
54}
55
56pub fn to_json_result(res: TaudResult<JsonValue>, id: u16) -> JsonResult {
57    match res {
58        Ok(v) => JsonResponse::new(v, id).into(),
59        Err(err) => match err {
60            TaudError::InvalidId => {
61                JsonError::new(ErrorCode::InvalidParams, Some("invalid task id".into()), id).into()
62            }
63            TaudError::InvalidData(e) | TaudError::JsonError(e) => {
64                JsonError::new(ErrorCode::InvalidParams, Some(e), id).into()
65            }
66            TaudError::InvalidDueTime => {
67                JsonError::new(ErrorCode::InvalidParams, Some("invalid due time".into()), id).into()
68            }
69            TaudError::EncryptionError(e) => {
70                JsonError::new(ErrorCode::InternalError, Some(e), id).into()
71            }
72            TaudError::DecryptionError(e) => {
73                JsonError::new(ErrorCode::InternalError, Some(e), id).into()
74            }
75            TaudError::Darkfi(e) => {
76                JsonError::new(ErrorCode::InternalError, Some(e.to_string()), id).into()
77            }
78            TaudError::IoError(e) => JsonError::new(ErrorCode::InternalError, Some(e), id).into(),
79        },
80    }
81}