error.rs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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. use darkfi::rpc::jsonrpc::{ErrorCode, JsonError, JsonResponse, JsonResult};
  19. use tinyjson::JsonValue;
  20. #[derive(Debug, thiserror::Error)]
  21. pub enum TaudError {
  22. #[error("Due timestamp invalid")]
  23. InvalidDueTime,
  24. #[error("Invalid Id")]
  25. InvalidId,
  26. #[error("Invalid Data/Params: `{0}` ")]
  27. InvalidData(String),
  28. #[error("InternalError")]
  29. Darkfi(#[from] darkfi::error::Error),
  30. #[error("Json serialization error: `{0}`")]
  31. JsonError(String),
  32. #[error("Encryption error: `{0}`")]
  33. EncryptionError(String),
  34. #[error("Decryption error: `{0}`")]
  35. DecryptionError(String),
  36. #[error("IO Error: `{0}`")]
  37. IoError(String),
  38. #[error("KVDB error: `{0}`")]
  39. Kvdb(#[from] kvdb_overlay::Error),
  40. }
  41. pub type TaudResult<T> = std::result::Result<T, TaudError>;
  42. impl From<crypto_box::aead::Error> for TaudError {
  43. fn from(err: crypto_box::aead::Error) -> TaudError {
  44. TaudError::EncryptionError(err.to_string())
  45. }
  46. }
  47. impl From<std::io::Error> for TaudError {
  48. fn from(err: std::io::Error) -> TaudError {
  49. TaudError::IoError(err.to_string())
  50. }
  51. }
  52. pub fn to_json_result(res: TaudResult<JsonValue>, id: i64) -> JsonResult {
  53. match res {
  54. Ok(v) => JsonResponse::new(v, id).into(),
  55. Err(err) => match err {
  56. TaudError::InvalidId => {
  57. JsonError::new(ErrorCode::InvalidParams, Some("invalid task id".into()), id).into()
  58. }
  59. TaudError::InvalidData(e) | TaudError::JsonError(e) => {
  60. JsonError::new(ErrorCode::InvalidParams, Some(e), id).into()
  61. }
  62. TaudError::InvalidDueTime => {
  63. JsonError::new(ErrorCode::InvalidParams, Some("invalid due time".into()), id).into()
  64. }
  65. TaudError::EncryptionError(e) => {
  66. JsonError::new(ErrorCode::InternalError, Some(e), id).into()
  67. }
  68. TaudError::DecryptionError(e) => {
  69. JsonError::new(ErrorCode::InternalError, Some(e), id).into()
  70. }
  71. TaudError::Darkfi(e) => {
  72. JsonError::new(ErrorCode::InternalError, Some(e.to_string()), id).into()
  73. }
  74. TaudError::IoError(e) => JsonError::new(ErrorCode::InternalError, Some(e), id).into(),
  75. TaudError::Kvdb(e) => {
  76. JsonError::new(ErrorCode::InternalError, Some(e.to_string()), id).into()
  77. }
  78. },
  79. }
  80. }