error.rs 2.9 KB

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