error.rs 3.1 KB

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