error.rs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //use serde_json::Value;
  2. //use darkfi::rpc::jsonrpc::{error as jsonerr, response as jsonresp, ErrorCode, JsonResult};
  3. #[derive(Debug, thiserror::Error)]
  4. pub enum DnetViewError {
  5. #[error("RPC reply is empty")]
  6. EmptyRpcReply,
  7. #[error("Json Value is not an object")]
  8. ValueIsNotObject,
  9. #[error("Failed to find ID at current index")]
  10. NoIdAtIndex,
  11. #[error("Message log does not contain ID")]
  12. CannotFindId,
  13. #[error("ID does not return a selectable object")]
  14. NotSelectableObject,
  15. #[error("JSON data does not contain an external addr")]
  16. NoExternalAddr,
  17. #[error("Found unexpected data in View")]
  18. UnexpectedData(String),
  19. #[error("InternalError")]
  20. Darkfi(#[from] darkfi::error::Error),
  21. #[error("Json serialization error: `{0}`")]
  22. SerdeJsonError(String),
  23. #[error("IO error: {0}")]
  24. Io(std::io::ErrorKind),
  25. #[error("SetLogger (log crate) failed: {0}")]
  26. SetLoggerError(String),
  27. #[error("URL parse error: {0}")]
  28. UrlParse(String),
  29. }
  30. pub type DnetViewResult<T> = std::result::Result<T, DnetViewError>;
  31. impl From<serde_json::Error> for DnetViewError {
  32. fn from(err: serde_json::Error) -> DnetViewError {
  33. DnetViewError::SerdeJsonError(err.to_string())
  34. }
  35. }
  36. impl From<std::io::Error> for DnetViewError {
  37. fn from(err: std::io::Error) -> Self {
  38. Self::Io(err.kind())
  39. }
  40. }
  41. impl From<log::SetLoggerError> for DnetViewError {
  42. fn from(err: log::SetLoggerError) -> Self {
  43. Self::SetLoggerError(err.to_string())
  44. }
  45. }
  46. impl From<url::ParseError> for DnetViewError {
  47. fn from(err: url::ParseError) -> Self {
  48. Self::UrlParse(err.to_string())
  49. }
  50. }
  51. //pub fn to_json_result(res: DnetViewResult<Value>, id: Value) -> JsonResult {
  52. // match res {
  53. // Ok(v) => JsonResult::Resp(jsonresp(v, id)),
  54. // Err(err) => match err {
  55. // DnetViewError::InvalidId => JsonResult::Err(jsonerr(
  56. // ErrorCode::InvalidParams,
  57. // Some("invalid task's id".into()),
  58. // id,
  59. // )),
  60. // DnetViewError::InvalidData(e) | DnetViewError::SerdeJsonError(e) => {
  61. // JsonResult::Err(jsonerr(ErrorCode::InvalidParams, Some(e), id))
  62. // }
  63. // DnetViewError::InvalidDueTime => JsonResult::Err(jsonerr(
  64. // ErrorCode::InvalidParams,
  65. // Some("invalid due time".into()),
  66. // id,
  67. // )),
  68. // DnetViewError::Darkfi(e) => {
  69. // JsonResult::Err(jsonerr(ErrorCode::InternalError, Some(e.to_string()), id))
  70. // }
  71. // },
  72. // }
  73. //}