error.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 AgentError {
  10. #[error("FATAL: No `main` window found")]
  11. NoMainWindow,
  12. #[error("Tauri error: {0}")]
  13. Tauri(#[from] tauri::Error),
  14. #[error("Invalid Registration")]
  15. InvalidRegistration,
  16. #[error("Invalid Client Public Key")]
  17. InvalidClientPublicKey,
  18. #[error("Unauthorized")]
  19. Unauthorized,
  20. #[error("Request not found or already completed")]
  21. RequestNotFound,
  22. #[error("Internal server error")]
  23. InternalServerError,
  24. #[error("Invalid request: {0}")]
  25. BadRequest(String),
  26. #[error("Client certificate error")]
  27. ClientCertError,
  28. #[error("Root certificate error")]
  29. RootCertError,
  30. #[error("Invalid method")]
  31. InvalidMethod,
  32. #[error("Invalid URL")]
  33. InvalidUrl,
  34. #[error("Invalid headers")]
  35. InvalidHeaders,
  36. #[error("Request run error: {0}")]
  37. RequestRunError(String),
  38. #[error("Request cancelled")]
  39. RequestCancelled,
  40. #[error("Failed to clear registrations")]
  41. RegistrationClearError,
  42. #[error("Failed to insert registrations")]
  43. RegistrationInsertError,
  44. #[error("Failed to save registrations to store")]
  45. RegistrationSaveError,
  46. #[error("Serde error: {0}")]
  47. Serde(#[from] serde_json::Error),
  48. #[error("Store error: {0}")]
  49. TauriPluginStore(#[from] tauri_plugin_store::Error),
  50. #[error("Relay error: {0}")]
  51. Relay(#[from] relay::error::RelayError),
  52. #[error("IO error: {0}")]
  53. Io(#[from] std::io::Error),
  54. #[error("Log init error: {0}")]
  55. LogInit(#[from] tracing_appender::rolling::InitError),
  56. #[error("Log init global error: {0}")]
  57. LogInitGlobal(#[from] tracing::subscriber::SetGlobalDefaultError),
  58. }
  59. impl IntoResponse for AgentError {
  60. fn into_response(self) -> Response {
  61. let (status, error_message) = match self {
  62. AgentError::InvalidRegistration => (StatusCode::BAD_REQUEST, self.to_string()),
  63. AgentError::InvalidClientPublicKey => (StatusCode::BAD_REQUEST, self.to_string()),
  64. AgentError::Unauthorized => (StatusCode::UNAUTHORIZED, self.to_string()),
  65. AgentError::RequestNotFound => (StatusCode::NOT_FOUND, self.to_string()),
  66. AgentError::InternalServerError => {
  67. (StatusCode::INTERNAL_SERVER_ERROR, self.to_string())
  68. }
  69. AgentError::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg),
  70. AgentError::ClientCertError => (StatusCode::BAD_REQUEST, self.to_string()),
  71. AgentError::RootCertError => (StatusCode::BAD_REQUEST, self.to_string()),
  72. AgentError::InvalidMethod => (StatusCode::BAD_REQUEST, self.to_string()),
  73. AgentError::InvalidUrl => (StatusCode::BAD_REQUEST, self.to_string()),
  74. AgentError::InvalidHeaders => (StatusCode::BAD_REQUEST, self.to_string()),
  75. AgentError::RequestRunError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
  76. AgentError::RequestCancelled => (StatusCode::BAD_REQUEST, self.to_string()),
  77. _ => (
  78. StatusCode::INTERNAL_SERVER_ERROR,
  79. "Internal Server Error".to_string(),
  80. ),
  81. };
  82. let body = Json(json!({
  83. "error": error_message,
  84. }));
  85. (status, body).into_response()
  86. }
  87. }
  88. pub type AgentResult<T> = std::result::Result<T, AgentError>;