error.rs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 serde_json::Value;
  19. use darkfi::rpc::jsonrpc::{ErrorCode, JsonError, JsonResponse, JsonResult};
  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. SerdeJsonError(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<serde_json::Error> for TaudError {
  41. fn from(err: serde_json::Error) -> TaudError {
  42. TaudError::SerdeJsonError(err.to_string())
  43. }
  44. }
  45. impl From<crypto_box::aead::Error> for TaudError {
  46. fn from(err: crypto_box::aead::Error) -> TaudError {
  47. TaudError::EncryptionError(err.to_string())
  48. }
  49. }
  50. impl From<std::io::Error> for TaudError {
  51. fn from(err: std::io::Error) -> TaudError {
  52. TaudError::IoError(err.to_string())
  53. }
  54. }
  55. pub fn to_json_result(res: TaudResult<Value>, id: Value) -> JsonResult {
  56. match res {
  57. Ok(v) => JsonResponse::new(v, id).into(),
  58. Err(err) => match err {
  59. TaudError::InvalidId => {
  60. JsonError::new(ErrorCode::InvalidParams, Some("invalid task id".into()), id).into()
  61. }
  62. TaudError::InvalidData(e) | TaudError::SerdeJsonError(e) => {
  63. JsonError::new(ErrorCode::InvalidParams, Some(e), id).into()
  64. }
  65. TaudError::InvalidDueTime => {
  66. JsonError::new(ErrorCode::InvalidParams, Some("invalid due time".into()), id).into()
  67. }
  68. TaudError::EncryptionError(e) => {
  69. JsonError::new(ErrorCode::InternalError, Some(e), id).into()
  70. }
  71. TaudError::DecryptionError(e) => {
  72. JsonError::new(ErrorCode::InternalError, Some(e), id).into()
  73. }
  74. TaudError::Darkfi(e) => {
  75. JsonError::new(ErrorCode::InternalError, Some(e.to_string()), id).into()
  76. }
  77. TaudError::IoError(e) => JsonError::new(ErrorCode::InternalError, Some(e), id).into(),
  78. },
  79. }
  80. }