error.rs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. use axum::{
  2. http::StatusCode,
  3. response::{IntoResponse, Response},
  4. Json,
  5. };
  6. use serde_json::json;
  7. use thiserror::Error;
  8. #[derive(Error, Debug)]
  9. pub enum AppError {
  10. #[error("Invalid Registration")]
  11. InvalidRegistration,
  12. #[error("Invalid Client Public Key")]
  13. InvalidClientPublicKey,
  14. #[error("Unauthorized")]
  15. Unauthorized,
  16. #[error("Request not found or already completed")]
  17. RequestNotFound,
  18. #[error("Internal server error")]
  19. InternalServerError,
  20. #[error("Invalid request: {0}")]
  21. BadRequest(String),
  22. #[error("Client certificate error")]
  23. ClientCertError,
  24. #[error("Root certificate error")]
  25. RootCertError,
  26. #[error("Invalid method")]
  27. InvalidMethod,
  28. #[error("Invalid URL")]
  29. InvalidUrl,
  30. #[error("Invalid headers")]
  31. InvalidHeaders,
  32. #[error("Request run error: {0}")]
  33. RequestRunError(String),
  34. #[error("Request cancelled")]
  35. RequestCancelled,
  36. #[error("Failed to clear registrations")]
  37. RegistrationClearError,
  38. #[error("Failed to insert registrations")]
  39. RegistrationInsertError,
  40. #[error("Failed to save registrations to store")]
  41. RegistrationSaveError,
  42. #[error("Store error: {0}")]
  43. TauriPluginStore(#[from] tauri_plugin_store::Error),
  44. #[error("Relay error: {0}")]
  45. Relay(#[from] hoppscotch_relay::RelayError),
  46. }
  47. impl IntoResponse for AppError {
  48. fn into_response(self) -> Response {
  49. let (status, error_message) = match self {
  50. AppError::InvalidRegistration => (StatusCode::BAD_REQUEST, self.to_string()),
  51. AppError::InvalidClientPublicKey => (StatusCode::BAD_REQUEST, self.to_string()),
  52. AppError::Unauthorized => (StatusCode::UNAUTHORIZED, self.to_string()),
  53. AppError::RequestNotFound => (StatusCode::NOT_FOUND, self.to_string()),
  54. AppError::InternalServerError => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
  55. AppError::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg),
  56. AppError::ClientCertError => (StatusCode::BAD_REQUEST, self.to_string()),
  57. AppError::RootCertError => (StatusCode::BAD_REQUEST, self.to_string()),
  58. AppError::InvalidMethod => (StatusCode::BAD_REQUEST, self.to_string()),
  59. AppError::InvalidUrl => (StatusCode::BAD_REQUEST, self.to_string()),
  60. AppError::InvalidHeaders => (StatusCode::BAD_REQUEST, self.to_string()),
  61. AppError::RequestRunError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
  62. AppError::RequestCancelled => (StatusCode::BAD_REQUEST, self.to_string()),
  63. _ => (
  64. StatusCode::INTERNAL_SERVER_ERROR,
  65. "Internal Server Error".to_string(),
  66. ),
  67. };
  68. let body = Json(json!({
  69. "error": error_message,
  70. }));
  71. (status, body).into_response()
  72. }
  73. }
  74. pub type AppResult<T> = std::result::Result<T, AppError>;