error.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. use cdk::nuts::nut17::ws::WsErrorBody;
  2. use serde::{Deserialize, Serialize};
  3. #[derive(Debug, Clone, Serialize, Deserialize)]
  4. /// Source: https://www.jsonrpc.org/specification#error_object
  5. pub enum WsError {
  6. /// Invalid JSON was received by the server.
  7. /// An error occurred on the server while parsing the JSON text.
  8. ParseError,
  9. /// The JSON sent is not a valid Request object.
  10. InvalidRequest,
  11. /// The method does not exist / is not available.
  12. MethodNotFound,
  13. /// Invalid method parameter(s).
  14. InvalidParams,
  15. /// Internal JSON-RPC error.
  16. InternalError,
  17. /// Custom error
  18. ServerError(i32, String),
  19. }
  20. impl From<WsError> for WsErrorBody {
  21. fn from(val: WsError) -> Self {
  22. let (id, message) = match val {
  23. WsError::ParseError => (-32700, "Parse error".to_string()),
  24. WsError::InvalidRequest => (-32600, "Invalid Request".to_string()),
  25. WsError::MethodNotFound => (-32601, "Method not found".to_string()),
  26. WsError::InvalidParams => (-32602, "Invalid params".to_string()),
  27. WsError::InternalError => (-32603, "Internal error".to_string()),
  28. WsError::ServerError(code, message) => (code, message),
  29. };
  30. WsErrorBody { code: id, message }
  31. }
  32. }