fix(query): preserve structured backend errors
This commit is contained in:
1 parent
2e9dcdc26b
commit
bcafc4a474
58 files changed
+4182
-554
No files matched your search
@@ -1,11 +1,14 @@
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::Json;
|
||||
use dbx_core::backend_error::BackendError;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AppError {
|
||||
pub message: String,
|
||||
pub status: StatusCode,
|
||||
pub error: Box<BackendError>,
|
||||
}
|
||||
|
||||
impl fmt::Display for AppError {
|
||||
@@ -16,32 +19,122 @@ impl fmt::Display for AppError {
|
||||
|
||||
impl AppError {
|
||||
pub fn internal(msg: impl Into<String>) -> Self {
|
||||
AppError { message: msg.into(), status: StatusCode::INTERNAL_SERVER_ERROR }
|
||||
Self::with_status(msg.into(), StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
pub fn bad_request(msg: impl Into<String>) -> Self {
|
||||
AppError { message: msg.into(), status: StatusCode::BAD_REQUEST }
|
||||
Self::with_status(msg.into(), StatusCode::BAD_REQUEST)
|
||||
}
|
||||
|
||||
pub fn not_found(msg: impl Into<String>) -> Self {
|
||||
AppError { message: msg.into(), status: StatusCode::NOT_FOUND }
|
||||
Self::with_status(msg.into(), StatusCode::NOT_FOUND)
|
||||
}
|
||||
|
||||
fn with_status(message: String, status: StatusCode) -> Self {
|
||||
let error = BackendError::from_legacy_string(&message);
|
||||
Self { message, status, error: Box::new(error) }
|
||||
}
|
||||
|
||||
pub fn from_backend_error(error: BackendError) -> Self {
|
||||
Self { message: error.code().to_string(), status: StatusCode::INTERNAL_SERVER_ERROR, error: Box::new(error) }
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
fn into_response(self) -> Response {
|
||||
(self.status, self.message).into_response()
|
||||
(self.status, Json(*self.error)).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for AppError {
|
||||
fn from(s: String) -> Self {
|
||||
AppError { message: s, status: StatusCode::INTERNAL_SERVER_ERROR }
|
||||
Self::internal(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for AppError {
|
||||
fn from(s: &str) -> Self {
|
||||
AppError { message: s.to_string(), status: StatusCode::INTERNAL_SERVER_ERROR }
|
||||
Self::internal(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BackendError> for AppError {
|
||||
fn from(error: BackendError) -> Self {
|
||||
Self::from_backend_error(error)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn legacy_errors_are_exposed_as_the_shared_backend_envelope() {
|
||||
let error = AppError::internal("database failed");
|
||||
assert_eq!(error.status, StatusCode::INTERNAL_SERVER_ERROR);
|
||||
assert_eq!(error.error.version(), 1);
|
||||
assert_eq!(error.error.code(), "DBX-LEGACY-0001");
|
||||
assert_eq!(error.message, "database failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn app_error_stays_small_for_route_result_types() {
|
||||
assert!(std::mem::size_of::<AppError>() <= 64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structured_agent_text_keeps_catalog_identity_at_http_boundary() {
|
||||
let error = AppError::from(
|
||||
"Agent RPC error (-1): timed out\nDBX_AGENT_ERROR_DATA:{\"category\":\"timeout\",\"stage\":\"execute\"}",
|
||||
);
|
||||
assert_eq!(error.error.code(), "DBX-JDBC-9001");
|
||||
assert_eq!(error.error.source(), dbx_core::backend_error::BackendErrorSource::JdbcAgentLegacy);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn http_response_preserves_filtered_legacy_detail() {
|
||||
let response = AppError::internal("database failed").into_response();
|
||||
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||
assert_eq!(response.headers()[axum::http::header::CONTENT_TYPE], "application/json");
|
||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
let payload: serde_json::Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(payload["version"], 1);
|
||||
assert_eq!(payload["code"], "DBX-LEGACY-0001");
|
||||
assert_eq!(payload["messageKey"], "backendErrors.legacy");
|
||||
assert_eq!(payload["detail"], "database failed");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn http_response_preserves_filtered_structured_agent_detail() {
|
||||
use dbx_core::db::agent_driver::{
|
||||
AgentCallError, AgentErrorCategory, AgentErrorContext, AgentErrorStage, AgentOperationOutcome,
|
||||
AgentSessionDisposition,
|
||||
};
|
||||
|
||||
let error = BackendError::from_agent_call_error(&AgentCallError::Structured {
|
||||
rpc_code: -1,
|
||||
message: "relation customer_orders does not exist".to_string(),
|
||||
context: AgentErrorContext {
|
||||
contract_version: 1,
|
||||
category: AgentErrorCategory::Sql,
|
||||
retryable: false,
|
||||
session_disposition: AgentSessionDisposition::Keep,
|
||||
stage: AgentErrorStage::Execute,
|
||||
operation_outcome: AgentOperationOutcome::Unknown,
|
||||
agent_session_id: Some("session-1".to_string()),
|
||||
sql_state: Some("42P01".to_string()),
|
||||
vendor_code: None,
|
||||
exception_class: Some("java.sql.SQLException".to_string()),
|
||||
},
|
||||
});
|
||||
|
||||
let response = AppError::from(error).into_response();
|
||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
let payload: serde_json::Value = serde_json::from_slice(&body).unwrap();
|
||||
|
||||
assert_eq!(payload["code"], "DBX-JDBC-4001");
|
||||
assert_eq!(payload["source"], "jdbcAgent");
|
||||
assert_eq!(payload["detail"], "relation customer_orders does not exist");
|
||||
assert!(payload["diagnostics"].get("agentSessionId").is_none());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user