error.rs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 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 std::sync::Arc;
  19. use darkfi::{
  20. error::RpcError,
  21. rpc::jsonrpc::{ErrorCode, JsonError},
  22. Error,
  23. };
  24. // Constant for the error code
  25. pub const ERROR_CODE_PING_DARKFID_FAILED: i32 = -32300;
  26. /// Custom RPC errors available for blockchain explorer.
  27. /// These represent specific RPC-related failures.
  28. #[derive(Debug, thiserror::Error)]
  29. pub enum ExplorerdError {
  30. #[error("Ping darkfid failed: {0}")]
  31. PingDarkfidFailed(String),
  32. #[error("Invalid contract ID: {0}")]
  33. InvalidContractId(String),
  34. #[error("Invalid header hash: {0}")]
  35. InvalidHeaderHash(String),
  36. #[error("Invalid tx hash: {0}")]
  37. InvalidTxHash(String),
  38. }
  39. /// Provides a conversion from [`ExplorerdError`] to darkfi [`Error`] type.
  40. impl From<ExplorerdError> for Error {
  41. fn from(err: ExplorerdError) -> Self {
  42. let error: RpcError = err.into();
  43. error.into()
  44. }
  45. }
  46. /// Conversion from [`ExplorerdRpcError`] to [`RpcError`]
  47. impl From<ExplorerdError> for RpcError {
  48. fn from(err: ExplorerdError) -> Self {
  49. RpcError::ServerError(Arc::new(err))
  50. }
  51. }
  52. /// Helper function to convert `ExplorerdRpcError` into error code with corresponding error message.
  53. pub fn to_error_code_message(e: &ExplorerdError) -> (i32, String) {
  54. match e {
  55. ExplorerdError::PingDarkfidFailed(_) => (ERROR_CODE_PING_DARKFID_FAILED, e.to_string()),
  56. ExplorerdError::InvalidContractId(_) |
  57. ExplorerdError::InvalidHeaderHash(_) |
  58. ExplorerdError::InvalidTxHash(_) => (ErrorCode::InvalidParams.code(), e.to_string()),
  59. }
  60. }
  61. /// Constructs a [`JsonError`] representing a server error using the provided
  62. /// [`ExplorerdError`] , request ID, and optional custom message, returning a [`JsonError`]
  63. /// with a corresponding server error code and message.
  64. pub fn server_error(e: &ExplorerdError, id: u16, msg: Option<&str>) -> JsonError {
  65. let (code, default_msg) = to_error_code_message(e);
  66. // Use the provided custom message if available; otherwise, use the default.
  67. let message = msg.unwrap_or(&default_msg).to_string();
  68. JsonError::new(ErrorCode::ServerError(code), Some(message), id)
  69. }