error.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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("Found unexpected data in View")]
  12. UnexpectedData,
  13. #[error("Message log does not contain ID")]
  14. CannotFindId,
  15. #[error("ID does not return a selectable object")]
  16. NotSelectableObject,
  17. #[error("InternalError")]
  18. Darkfi(#[from] darkfi::error::Error),
  19. #[error("Json serialization error: `{0}`")]
  20. SerdeJsonError(String),
  21. #[error("IO error: {0}")]
  22. Io(std::io::ErrorKind),
  23. #[error("SetLogger (log crate) failed: {0}")]
  24. SetLoggerError(String),
  25. #[error("URL parse error: {0}")]
  26. UrlParse(String),
  27. }
  28. pub type DnetViewResult<T> = std::result::Result<T, DnetViewError>;
  29. impl From<serde_json::Error> for DnetViewError {
  30. fn from(err: serde_json::Error) -> DnetViewError {
  31. DnetViewError::SerdeJsonError(err.to_string())
  32. }
  33. }
  34. impl From<std::io::Error> for DnetViewError {
  35. fn from(err: std::io::Error) -> Self {
  36. Self::Io(err.kind())
  37. }
  38. }
  39. impl From<log::SetLoggerError> for DnetViewError {
  40. fn from(err: log::SetLoggerError) -> Self {
  41. Self::SetLoggerError(err.to_string())
  42. }
  43. }
  44. impl From<url::ParseError> for DnetViewError {
  45. fn from(err: url::ParseError) -> Self {
  46. Self::UrlParse(err.to_string())
  47. }
  48. }
  49. //pub fn to_json_result(res: DnetViewResult<Value>, id: Value) -> JsonResult {
  50. // match res {
  51. // Ok(v) => JsonResult::Resp(jsonresp(v, id)),
  52. // Err(err) => match err {
  53. // DnetViewError::InvalidId => JsonResult::Err(jsonerr(
  54. // ErrorCode::InvalidParams,
  55. // Some("invalid task's id".into()),
  56. // id,
  57. // )),
  58. // DnetViewError::InvalidData(e) | DnetViewError::SerdeJsonError(e) => {
  59. // JsonResult::Err(jsonerr(ErrorCode::InvalidParams, Some(e), id))
  60. // }
  61. // DnetViewError::InvalidDueTime => JsonResult::Err(jsonerr(
  62. // ErrorCode::InvalidParams,
  63. // Some("invalid due time".into()),
  64. // id,
  65. // )),
  66. // DnetViewError::Darkfi(e) => {
  67. // JsonResult::Err(jsonerr(ErrorCode::InternalError, Some(e.to_string()), id))
  68. // }
  69. // },
  70. // }
  71. //}