error.rs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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("IO Error: `{0}`")]
  35. IoError(String),
  36. }
  37. pub type TaudResult<T> = std::result::Result<T, TaudError>;
  38. impl From<serde_json::Error> for TaudError {
  39. fn from(err: serde_json::Error) -> TaudError {
  40. TaudError::SerdeJsonError(err.to_string())
  41. }
  42. }
  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<Value>, id: Value) -> 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::SerdeJsonError(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::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. }