1use axum::Json;
2use axum::extract::rejection::JsonRejection;
3use axum::http::StatusCode;
4use axum::response::{IntoResponse, Response};
5use crw_core::error::CrwError;
6use crw_core::types::ApiResponse;
7
8pub struct AppError(pub CrwError);
10
11impl From<CrwError> for AppError {
12 fn from(e: CrwError) -> Self {
13 Self(e)
14 }
15}
16
17impl From<JsonRejection> for AppError {
18 fn from(rejection: JsonRejection) -> Self {
19 let msg = match &rejection {
20 JsonRejection::JsonDataError(_) => {
21 let raw = rejection.body_text();
22 if let Some(pos) = raw.find(": ") {
24 format!("Invalid request body: {}", &raw[pos + 2..])
25 } else {
26 format!("Invalid request body: {raw}")
27 }
28 }
29 JsonRejection::JsonSyntaxError(_) => "Invalid JSON syntax in request body".to_string(),
30 JsonRejection::MissingJsonContentType(_) => {
31 "Missing Content-Type: application/json header".to_string()
32 }
33 _ => "Invalid request body".to_string(),
34 };
35 Self(CrwError::InvalidRequest(msg))
36 }
37}
38
39impl IntoResponse for AppError {
40 fn into_response(self) -> Response {
41 let status = match &self.0 {
42 CrwError::InvalidRequest(_) => StatusCode::BAD_REQUEST,
43 CrwError::NotFound(_) => StatusCode::NOT_FOUND,
44 CrwError::Timeout(_) => StatusCode::GATEWAY_TIMEOUT,
45 CrwError::HttpError(_) => StatusCode::BAD_GATEWAY,
46 CrwError::TargetUnreachable(_) => StatusCode::UNPROCESSABLE_ENTITY,
47 CrwError::ExtractionError(_) => StatusCode::UNPROCESSABLE_ENTITY,
48 CrwError::RateLimited => StatusCode::TOO_MANY_REQUESTS,
49 CrwError::SearchDisabled(_) => StatusCode::SERVICE_UNAVAILABLE,
50 _ => StatusCode::INTERNAL_SERVER_ERROR,
51 };
52
53 let error_code = self.0.error_code().to_string();
54 let body = ApiResponse::<()>::err_with_code(self.0.to_string(), error_code);
55 (status, Json(body)).into_response()
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62 use axum::http::StatusCode;
63
64 fn status_for(err: CrwError) -> StatusCode {
65 let app_err = AppError(err);
66 let response = app_err.into_response();
67 response.status()
68 }
69
70 #[test]
71 fn app_error_invalid_request_400() {
72 assert_eq!(
73 status_for(CrwError::InvalidRequest("bad".into())),
74 StatusCode::BAD_REQUEST
75 );
76 }
77
78 #[test]
79 fn app_error_not_found_404() {
80 assert_eq!(
81 status_for(CrwError::NotFound("missing".into())),
82 StatusCode::NOT_FOUND
83 );
84 }
85
86 #[test]
87 fn app_error_timeout_504() {
88 assert_eq!(
89 status_for(CrwError::Timeout(5000)),
90 StatusCode::GATEWAY_TIMEOUT
91 );
92 }
93
94 #[test]
95 fn app_error_http_error_502() {
96 assert_eq!(
97 status_for(CrwError::HttpError("fail".into())),
98 StatusCode::BAD_GATEWAY
99 );
100 }
101
102 #[test]
103 fn app_error_extraction_422() {
104 assert_eq!(
105 status_for(CrwError::ExtractionError("parse fail".into())),
106 StatusCode::UNPROCESSABLE_ENTITY
107 );
108 }
109
110 #[test]
111 fn app_error_internal_500() {
112 assert_eq!(
113 status_for(CrwError::Internal("oops".into())),
114 StatusCode::INTERNAL_SERVER_ERROR
115 );
116 }
117
118 #[test]
119 fn app_error_renderer_500() {
120 assert_eq!(
121 status_for(CrwError::RendererError("cdp fail".into())),
122 StatusCode::INTERNAL_SERVER_ERROR
123 );
124 }
125
126 #[tokio::test]
127 async fn app_error_body_is_api_response() {
128 let app_err = AppError(CrwError::InvalidRequest("test error".into()));
129 let response = app_err.into_response();
130 assert_eq!(response.status(), StatusCode::BAD_REQUEST);
131
132 let body = axum::body::to_bytes(response.into_body(), 1024 * 1024)
133 .await
134 .unwrap();
135 let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
136 assert_eq!(json["success"], false);
137 assert!(json["error"].as_str().unwrap().contains("test error"));
138 }
139}