Skip to main content

reifydb_sub_server_http/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4//! HTTP error handling and response formatting.
5//!
6//! This module provides error types that implement Axum's `IntoResponse` trait
7//! for consistent error responses across all HTTP endpoints.
8
9use std::{error, fmt};
10
11use axum::{
12	Json,
13	http::StatusCode,
14	response::{IntoResponse, Response},
15};
16use reifydb_sub_server::{auth::AuthError, execute::ExecuteError};
17use reifydb_type::error::Diagnostic;
18use serde::Serialize;
19use tracing::{debug, error};
20
21/// JSON error response body.
22#[derive(Debug, Serialize)]
23pub struct ErrorResponse {
24	/// Human-readable error message.
25	pub error: String,
26	/// Machine-readable error code.
27	pub code: String,
28}
29
30impl ErrorResponse {
31	pub fn new(code: impl Into<String>, error: impl Into<String>) -> Self {
32		Self {
33			code: code.into(),
34			error: error.into(),
35		}
36	}
37}
38
39/// JSON diagnostic error response body (matches WS format).
40#[derive(Debug, Serialize)]
41pub struct DiagnosticResponse {
42	/// Full diagnostic information.
43	pub diagnostic: Diagnostic,
44}
45
46/// Application error type that converts to HTTP responses.
47#[derive(Debug)]
48pub enum AppError {
49	/// Authentication error.
50	Auth(AuthError),
51	/// Query/command execution error.
52	Execute(ExecuteError),
53	/// Request parsing error.
54	BadRequest(String),
55	/// Invalid parameter error.
56	InvalidParams(String),
57	/// Resource not found (404).
58	NotFound(String),
59	/// HTTP method not allowed for this resource (405).
60	MethodNotAllowed(String),
61	/// Internal server error.
62	Internal(String),
63}
64
65impl From<AuthError> for AppError {
66	fn from(e: AuthError) -> Self {
67		AppError::Auth(e)
68	}
69}
70
71impl From<ExecuteError> for AppError {
72	fn from(e: ExecuteError) -> Self {
73		AppError::Execute(e)
74	}
75}
76
77impl fmt::Display for AppError {
78	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79		match self {
80			AppError::Auth(e) => write!(f, "Authentication error: {}", e),
81			AppError::Execute(e) => write!(f, "Execution error: {}", e),
82			AppError::BadRequest(msg) => write!(f, "Bad request: {}", msg),
83			AppError::InvalidParams(msg) => write!(f, "Invalid params: {}", msg),
84			AppError::NotFound(msg) => write!(f, "Not found: {}", msg),
85			AppError::MethodNotAllowed(msg) => write!(f, "Method not allowed: {}", msg),
86			AppError::Internal(msg) => write!(f, "Internal error: {}", msg),
87		}
88	}
89}
90
91impl error::Error for AppError {}
92
93impl IntoResponse for AppError {
94	fn into_response(self) -> Response {
95		// Handle engine errors specially - they have full diagnostic info
96		if let AppError::Execute(ExecuteError::Engine {
97			diagnostic,
98			rql,
99		}) = self
100		{
101			debug!("Engine error: {}", diagnostic.message);
102			let mut diag = (*diagnostic).clone();
103			if diag.rql.is_none() && !rql.is_empty() {
104				diag.with_rql(rql);
105			}
106			let body = Json(DiagnosticResponse {
107				diagnostic: diag,
108			});
109			return (StatusCode::BAD_REQUEST, body).into_response();
110		}
111
112		let (status, code, message) = match &self {
113			AppError::Auth(AuthError::MissingCredentials) => {
114				(StatusCode::UNAUTHORIZED, "AUTH_REQUIRED", "Authentication required")
115			}
116			AppError::Auth(AuthError::InvalidToken) => {
117				(StatusCode::UNAUTHORIZED, "INVALID_TOKEN", "Invalid authentication token")
118			}
119			AppError::Auth(AuthError::Expired) => {
120				(StatusCode::UNAUTHORIZED, "TOKEN_EXPIRED", "Authentication token expired")
121			}
122			AppError::Auth(AuthError::InvalidHeader) => {
123				(StatusCode::BAD_REQUEST, "INVALID_HEADER", "Malformed authorization header")
124			}
125			AppError::Auth(AuthError::InsufficientPermissions) => {
126				(StatusCode::FORBIDDEN, "FORBIDDEN", "Insufficient permissions for this operation")
127			}
128			AppError::Execute(ExecuteError::Timeout) => {
129				(StatusCode::GATEWAY_TIMEOUT, "QUERY_TIMEOUT", "Query execution timed out")
130			}
131			AppError::Execute(ExecuteError::Cancelled) => {
132				(StatusCode::BAD_REQUEST, "QUERY_CANCELLED", "Query was cancelled")
133			}
134			AppError::Execute(ExecuteError::Disconnected) => {
135				error!("Query stream disconnected unexpectedly");
136				(StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL_ERROR", "Internal server error")
137			}
138			AppError::Execute(ExecuteError::Rejected {
139				code,
140				message,
141			}) => {
142				let body = Json(ErrorResponse::new(code, message));
143				return (StatusCode::FORBIDDEN, body).into_response();
144			}
145			AppError::Execute(ExecuteError::Engine {
146				..
147			}) => {
148				// Already handled above
149				unreachable!()
150			}
151			AppError::BadRequest(msg) => {
152				let body = Json(ErrorResponse::new("BAD_REQUEST", msg.clone()));
153				return (StatusCode::BAD_REQUEST, body).into_response();
154			}
155			AppError::InvalidParams(msg) => {
156				let body = Json(ErrorResponse::new("INVALID_PARAMS", msg.clone()));
157				return (StatusCode::BAD_REQUEST, body).into_response();
158			}
159			AppError::NotFound(msg) => {
160				let body = Json(ErrorResponse::new("NOT_FOUND", msg.clone()));
161				return (StatusCode::NOT_FOUND, body).into_response();
162			}
163			AppError::MethodNotAllowed(msg) => {
164				let body = Json(ErrorResponse::new("METHOD_NOT_ALLOWED", msg.clone()));
165				return (StatusCode::METHOD_NOT_ALLOWED, body).into_response();
166			}
167			AppError::Internal(msg) => {
168				error!("Internal error: {}", msg);
169				(StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL_ERROR", "Internal server error")
170			}
171		};
172
173		let body = Json(ErrorResponse::new(code, message));
174		(status, body).into_response()
175	}
176}
177
178#[cfg(test)]
179pub mod tests {
180	use serde_json::to_string;
181
182	use super::*;
183
184	#[test]
185	fn test_error_response_serialization() {
186		let resp = ErrorResponse::new("TEST_CODE", "Test error message");
187		let json = to_string(&resp).unwrap();
188		assert!(json.contains("TEST_CODE"));
189		assert!(json.contains("Test error message"));
190	}
191
192	#[test]
193	fn test_app_error_display() {
194		let err = AppError::BadRequest("Invalid JSON".to_string());
195		assert_eq!(err.to_string(), "Bad request: Invalid JSON");
196	}
197}