Skip to main content

reifydb_sub_server_http/
error.rs

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