reifydb_sub_server_http/
error.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later
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 axum::{
10	Json,
11	http::StatusCode,
12	response::{IntoResponse, Response},
13};
14use reifydb_sub_server::{AuthError, ExecuteError};
15use reifydb_type::diagnostic::Diagnostic;
16use serde::Serialize;
17
18/// JSON error response body.
19#[derive(Debug, Serialize)]
20pub struct ErrorResponse {
21	/// Human-readable error message.
22	pub error: String,
23	/// Machine-readable error code.
24	pub code: String,
25}
26
27impl ErrorResponse {
28	pub fn new(code: impl Into<String>, error: impl Into<String>) -> Self {
29		Self {
30			code: code.into(),
31			error: error.into(),
32		}
33	}
34}
35
36/// JSON diagnostic error response body (matches WS format).
37#[derive(Debug, Serialize)]
38pub struct DiagnosticResponse {
39	/// Full diagnostic information.
40	pub diagnostic: Diagnostic,
41}
42
43/// Application error type that converts to HTTP responses.
44#[derive(Debug)]
45pub enum AppError {
46	/// Authentication error.
47	Auth(AuthError),
48	/// Query/command execution error.
49	Execute(ExecuteError),
50	/// Request parsing error.
51	BadRequest(String),
52	/// Internal server error.
53	Internal(String),
54}
55
56impl From<AuthError> for AppError {
57	fn from(e: AuthError) -> Self {
58		AppError::Auth(e)
59	}
60}
61
62impl From<ExecuteError> for AppError {
63	fn from(e: ExecuteError) -> Self {
64		AppError::Execute(e)
65	}
66}
67
68impl std::fmt::Display for AppError {
69	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70		match self {
71			AppError::Auth(e) => write!(f, "Authentication error: {}", e),
72			AppError::Execute(e) => write!(f, "Execution error: {}", e),
73			AppError::BadRequest(msg) => write!(f, "Bad request: {}", msg),
74			AppError::Internal(msg) => write!(f, "Internal error: {}", msg),
75		}
76	}
77}
78
79impl std::error::Error for AppError {}
80
81impl IntoResponse for AppError {
82	fn into_response(self) -> Response {
83		// Handle engine errors specially - they need ownership of error for diagnostic()
84		if let AppError::Execute(ExecuteError::Engine {
85			error,
86			statement,
87		}) = self
88		{
89			tracing::debug!("Engine error: {}", error);
90			let mut diagnostic = error.diagnostic();
91			diagnostic.with_statement(statement);
92			let body = Json(DiagnosticResponse {
93				diagnostic,
94			});
95			return (StatusCode::BAD_REQUEST, body).into_response();
96		}
97
98		let (status, code, message) = match &self {
99			AppError::Auth(AuthError::MissingCredentials) => {
100				(StatusCode::UNAUTHORIZED, "AUTH_REQUIRED", "Authentication required")
101			}
102			AppError::Auth(AuthError::InvalidToken) => {
103				(StatusCode::UNAUTHORIZED, "INVALID_TOKEN", "Invalid authentication token")
104			}
105			AppError::Auth(AuthError::Expired) => {
106				(StatusCode::UNAUTHORIZED, "TOKEN_EXPIRED", "Authentication token expired")
107			}
108			AppError::Auth(AuthError::InvalidHeader) => {
109				(StatusCode::BAD_REQUEST, "INVALID_HEADER", "Malformed authorization header")
110			}
111			AppError::Auth(AuthError::InsufficientPermissions) => {
112				(StatusCode::FORBIDDEN, "FORBIDDEN", "Insufficient permissions for this operation")
113			}
114			AppError::Execute(ExecuteError::Timeout) => {
115				(StatusCode::GATEWAY_TIMEOUT, "QUERY_TIMEOUT", "Query execution timed out")
116			}
117			AppError::Execute(ExecuteError::TaskPanic(msg)) => {
118				tracing::error!("Query task panicked: {}", msg);
119				(StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL_ERROR", "Internal server error")
120			}
121			AppError::Execute(ExecuteError::Engine {
122				..
123			}) => {
124				// Already handled above
125				unreachable!()
126			}
127			AppError::BadRequest(msg) => {
128				let body = Json(ErrorResponse::new("BAD_REQUEST", msg.clone()));
129				return (StatusCode::BAD_REQUEST, body).into_response();
130			}
131			AppError::Internal(msg) => {
132				tracing::error!("Internal error: {}", msg);
133				(StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL_ERROR", "Internal server error")
134			}
135		};
136
137		let body = Json(ErrorResponse::new(code, message));
138		(status, body).into_response()
139	}
140}
141
142#[cfg(test)]
143mod tests {
144	use super::*;
145
146	#[test]
147	fn test_error_response_serialization() {
148		let resp = ErrorResponse::new("TEST_CODE", "Test error message");
149		let json = serde_json::to_string(&resp).unwrap();
150		assert!(json.contains("TEST_CODE"));
151		assert!(json.contains("Test error message"));
152	}
153
154	#[test]
155	fn test_app_error_display() {
156		let err = AppError::BadRequest("Invalid JSON".to_string());
157		assert_eq!(err.to_string(), "Bad request: Invalid JSON");
158	}
159}