error.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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. }
  39. pub type TaudResult<T> = std::result::Result<T, TaudError>;
  40. impl From<crypto_box::aead::Error> for TaudError {
  41. fn from(err: crypto_box::aead::Error) -> TaudError {
  42. TaudError::EncryptionError(err.to_string())
  43. }
  44. }
  45. impl From<std::io::Error> for TaudError {
  46. fn from(err: std::io::Error) -> TaudError {
  47. TaudError::IoError(err.to_string())
  48. }
  49. }
  50. pub fn to_json_result(res: TaudResult<JsonValue>, id: u16) -> JsonResult {
  51. match res {
  52. Ok(v) => JsonResponse::new(v, id).into(),
  53. Err(err) => match err {
  54. TaudError::InvalidId => {
  55. JsonError::new(ErrorCode::InvalidParams, Some("invalid task id".into()), id).into()
  56. }
  57. TaudError::InvalidData(e) | TaudError::JsonError(e) => {
  58. JsonError::new(ErrorCode::InvalidParams, Some(e), id).into()
  59. }
  60. TaudError::InvalidDueTime => {
  61. JsonError::new(ErrorCode::InvalidParams, Some("invalid due time".into()), id).into()
  62. }
  63. TaudError::EncryptionError(e) => {
  64. JsonError::new(ErrorCode::InternalError, Some(e), id).into()
  65. }
  66. TaudError::DecryptionError(e) => {
  67. JsonError::new(ErrorCode::InternalError, Some(e), id).into()
  68. }
  69. TaudError::Darkfi(e) => {
  70. JsonError::new(ErrorCode::InternalError, Some(e.to_string()), id).into()
  71. }
  72. TaudError::IoError(e) => JsonError::new(ErrorCode::InternalError, Some(e), id).into(),
  73. },
  74. }
  75. }