feat(ai): add custom prompt templates

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-07-22 13:43:39 +08:00
1 parent 01fde8879f
commit 1fb3e2816b
58 files changed
+2381 -502

No files matched your search

+13 -6
View File
@@ -1,32 +1,39 @@
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
pub struct AppError(pub String);
pub struct AppError {
pub message: String,
pub status: StatusCode,
}
impl AppError {
pub fn internal(msg: impl Into<String>) -> Self {
AppError(msg.into())
AppError { message: msg.into(), status: StatusCode::INTERNAL_SERVER_ERROR }
}
pub fn bad_request(msg: impl Into<String>) -> Self {
AppError(msg.into())
AppError { message: msg.into(), status: StatusCode::BAD_REQUEST }
}
pub fn not_found(msg: impl Into<String>) -> Self {
AppError { message: msg.into(), status: StatusCode::NOT_FOUND }
}
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, self.0).into_response()
(self.status, self.message).into_response()
}
}
impl From<String> for AppError {
fn from(s: String) -> Self {
AppError(s)
AppError { message: s, status: StatusCode::INTERNAL_SERVER_ERROR }
}
}
impl From<&str> for AppError {
fn from(s: &str) -> Self {
AppError(s.to_string())
AppError { message: s.to_string(), status: StatusCode::INTERNAL_SERVER_ERROR }
}
}
+10
View File
@@ -517,6 +517,16 @@ async fn main() {
.route("/ai/cancel-stream", post(routes::ai::ai_cancel_stream))
.route("/ai/test-connection", post(routes::ai::ai_test_connection))
.route("/ai/models", post(routes::ai::ai_list_models))
// Prompt templates
.route(
"/prompt-templates",
get(routes::prompt_template::load_prompt_templates).post(routes::prompt_template::save_prompt_template),
)
.route("/prompt-templates/{id}", delete(routes::prompt_template::delete_prompt_template))
.route(
"/prompt-templates/global-instructions",
get(routes::prompt_template::get_global_instructions).put(routes::prompt_template::set_global_instructions),
)
// Transfer
.route("/transfer/start", post(routes::transfer::start_transfer))
.route("/transfer/ownership-preview", post(routes::transfer::preview_transfer_ownership))
+32 -32
View File
@@ -64,7 +64,7 @@ pub async fn get_driver_store_usage(State(state): State<Arc<WebState>>) -> Resul
pub async fn clear_driver_download_cache(
State(state): State<Arc<WebState>>,
) -> Result<Json<serde_json::Value>, AppError> {
clear_agent_download_cache(&state.app.agent_manager).map_err(AppError)?;
clear_agent_download_cache(&state.app.agent_manager).map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
@@ -84,7 +84,7 @@ pub async fn stop_driver_runtime(
State(state): State<Arc<WebState>>,
Json(req): Json<DriverRuntimeRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
dbx_core::driver_runtime::stop_driver_runtime(&state.app, &req.runtime_id).await.map_err(AppError)?;
dbx_core::driver_runtime::stop_driver_runtime(&state.app, &req.runtime_id).await.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
@@ -92,7 +92,7 @@ pub async fn restart_driver_runtime(
State(state): State<Arc<WebState>>,
Json(req): Json<DriverRuntimeRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
dbx_core::driver_runtime::restart_driver_runtime(&state.app, &req.runtime_id).await.map_err(AppError)?;
dbx_core::driver_runtime::restart_driver_runtime(&state.app, &req.runtime_id).await.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
@@ -100,32 +100,32 @@ pub async fn install_agent(
State(state): State<Arc<WebState>>,
Json(req): Json<AgentTypeRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&req.db_type)).await.map_err(AppError)?;
ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&req.db_type)).await.map_err(AppError::from)?;
let tx = progress_sender(&state, "global").await;
install_agent_driver(&state.app.agent_manager, &req.db_type, |event| send_progress_event(&tx, event))
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
pub async fn upgrade_all_agents(State(state): State<Arc<WebState>>) -> Result<Json<serde_json::Value>, AppError> {
let registry = fetch_registry().await.map_err(AppError)?;
let registry = fetch_registry().await.map_err(AppError::from)?;
let agents = build_agent_list(&state.app.agent_manager, Some(&registry));
let updatable: Vec<String> =
agents.iter().filter(|agent| agent.update_available).map(|agent| agent.db_type.clone()).collect();
ensure_no_agent_update_blockers(&state.app, &updatable).await.map_err(AppError)?;
ensure_no_agent_update_blockers(&state.app, &updatable).await.map_err(AppError::from)?;
let tx = progress_sender(&state, "global").await;
let result = upgrade_all_agent_drivers(&state.app.agent_manager, |event| send_progress_event(&tx, event))
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|err| AppError(err.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|err| AppError::from(err.to_string()))?))
}
pub async fn uninstall_agent(
State(state): State<Arc<WebState>>,
Json(req): Json<AgentTypeRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
uninstall_agent_driver(&state.app.agent_manager, &req.db_type).await.map_err(AppError)?;
uninstall_agent_driver(&state.app.agent_manager, &req.db_type).await.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
@@ -143,7 +143,7 @@ pub async fn set_agent_java_runtime_config(
let mut config = req.config;
if config.mode == JavaRuntimeMode::Custom || config.mode == JavaRuntimeMode::System {
let candidate_state = AgentState { java_runtime: config.clone(), ..am.load_state() };
let resolved = am.resolve_java_runtime(&candidate_state, DEFAULT_JRE_KEY).map_err(AppError)?;
let resolved = am.resolve_java_runtime(&candidate_state, DEFAULT_JRE_KEY).map_err(AppError::from)?;
if config.mode == JavaRuntimeMode::Custom {
config.custom_java_path = Some(resolved.to_string_lossy().to_string());
}
@@ -154,7 +154,7 @@ pub async fn set_agent_java_runtime_config(
let mut local_state = am.load_state();
local_state.java_runtime = config.clone();
am.save_state(&local_state).map_err(AppError)?;
am.save_state(&local_state).map_err(AppError::from)?;
am.stop_daemons().await;
Ok(Json(config))
}
@@ -169,29 +169,29 @@ pub async fn import_agents_from_zip(
mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, AppError> {
let tmp_dir = state.data_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir).map_err(|err| AppError(err.to_string()))?;
std::fs::create_dir_all(&tmp_dir).map_err(|err| AppError::from(err.to_string()))?;
if let Some(field) = multipart.next_field().await.map_err(|err| AppError(err.to_string()))? {
if let Some(field) = multipart.next_field().await.map_err(|err| AppError::from(err.to_string()))? {
let file_name = field.file_name().unwrap_or("offline-drivers.zip").to_string();
if !file_name.to_ascii_lowercase().ends_with(".zip") {
return Err(AppError("Offline driver package must be a .zip file".to_string()));
return Err(AppError::from("Offline driver package must be a .zip file".to_string()));
}
let zip_path = tmp_dir.join(format!("agent-offline-{}.zip", uuid::Uuid::new_v4()));
let tx = progress_sender(&state, "global").await;
let result = async {
let mut upload = tokio::fs::File::create(&zip_path).await.map_err(|err| AppError(err.to_string()))?;
let mut upload = tokio::fs::File::create(&zip_path).await.map_err(|err| AppError::from(err.to_string()))?;
let mut field = field;
while let Some(chunk) = field.chunk().await.map_err(|err| AppError(err.to_string()))? {
upload.write_all(&chunk).await.map_err(|err| AppError(err.to_string()))?;
while let Some(chunk) = field.chunk().await.map_err(|err| AppError::from(err.to_string()))? {
upload.write_all(&chunk).await.map_err(|err| AppError::from(err.to_string()))?;
}
upload.flush().await.map_err(|err| AppError(err.to_string()))?;
upload.flush().await.map_err(|err| AppError::from(err.to_string()))?;
drop(upload);
let plan = inspect_offline_zip(&zip_path).map_err(AppError)?;
ensure_no_offline_import_blockers(&state.app, &plan).await.map_err(AppError)?;
let plan = inspect_offline_zip(&zip_path).map_err(AppError::from)?;
ensure_no_offline_import_blockers(&state.app, &plan).await.map_err(AppError::from)?;
import_agents_from_zip_core(&state.app.agent_manager, &zip_path, |event| send_progress_event(&tx, event))
.map_err(AppError)
.map_err(AppError::from)
}
.await;
let _ = std::fs::remove_file(&zip_path);
@@ -201,7 +201,7 @@ pub async fn import_agents_from_zip(
return Ok(Json(serde_json::json!({ "count": result.drivers_installed.len() as u32 })));
}
Err(AppError("No file uploaded".to_string()))
Err(AppError::from("No file uploaded".to_string()))
}
pub async fn import_agent_driver_file(
@@ -215,28 +215,28 @@ pub async fn import_agent_driver_file(
while let Ok(Some(field)) = multipart.next_field().await {
let name = field.name().unwrap_or("").to_string();
if name == "dbType" {
db_type = Some(field.text().await.map_err(|e| AppError(e.to_string()))?);
db_type = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?);
} else if name == "file" {
driver_name = field.file_name().unwrap_or("agent").to_string();
driver_data = Some(field.bytes().await.map_err(|e| AppError(e.to_string()))?.to_vec());
driver_data = Some(field.bytes().await.map_err(|e| AppError::from(e.to_string()))?.to_vec());
}
}
let db_type = db_type.ok_or_else(|| AppError("Missing dbType field".to_string()))?;
let data = driver_data.ok_or_else(|| AppError("No file uploaded".to_string()))?;
let db_type = db_type.ok_or_else(|| AppError::from("Missing dbType field".to_string()))?;
let data = driver_data.ok_or_else(|| AppError::from("No file uploaded".to_string()))?;
let temp_dir = state.app.plugins.root_dir().join("agent_upload_tmp");
std::fs::create_dir_all(&temp_dir).map_err(|e| AppError(e.to_string()))?;
std::fs::create_dir_all(&temp_dir).map_err(|e| AppError::from(e.to_string()))?;
let suffix = std::path::Path::new(&driver_name)
.extension()
.and_then(|extension| extension.to_str())
.map(|extension| format!(".{extension}"))
.unwrap_or_default();
let tmp_path = temp_dir.join(format!("agent-{}{}", uuid::Uuid::new_v4(), suffix));
std::fs::write(&tmp_path, &data).map_err(|e| AppError(e.to_string()))?;
std::fs::write(&tmp_path, &data).map_err(|e| AppError::from(e.to_string()))?;
let result = async {
ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&db_type)).await.map_err(AppError)?;
ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&db_type)).await.map_err(AppError::from)?;
import_agent_driver(&state.app.agent_manager, &db_type, &tmp_path).map_err(AppError::from)
}
.await;
@@ -254,7 +254,7 @@ pub async fn reinstall_jre(
send_progress_event(&tx, event);
})
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
@@ -263,7 +263,7 @@ pub async fn uninstall_jre(
Json(req): Json<JreRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
let key = req.jre_key.as_deref().unwrap_or(DEFAULT_JRE_KEY);
uninstall_agent_jre(&state.app.agent_manager, key).await.map_err(AppError)?;
uninstall_agent_jre(&state.app.agent_manager, key).await.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
+16 -16
View File
@@ -113,12 +113,12 @@ pub async fn save_ai_config(
Json(body): Json<SaveAiConfigRequest>,
) -> Result<Json<()>, AppError> {
reject_web_unsupported_ai_provider(&body.config)?;
state.app.storage.save_ai_config(&body.config).await.map_err(AppError)?;
state.app.storage.save_ai_config(&body.config).await.map_err(AppError::from)?;
Ok(Json(()))
}
pub async fn load_ai_config(State(state): State<Arc<WebState>>) -> Result<Json<Option<AiConfig>>, AppError> {
let config = state.app.storage.load_ai_config().await.map_err(AppError)?;
let config = state.app.storage.load_ai_config().await.map_err(AppError::from)?;
Ok(Json(config))
}
@@ -130,14 +130,14 @@ pub async fn save_ai_provider_config(
let mut config = body.config;
config.provider = parsed_provider;
reject_web_unsupported_ai_provider(&config)?;
state.app.storage.save_ai_provider_config(&body.provider, &config).await.map_err(AppError)?;
state.app.storage.save_ai_provider_config(&body.provider, &config).await.map_err(AppError::from)?;
Ok(Json(()))
}
pub async fn load_ai_provider_configs(
State(state): State<Arc<WebState>>,
) -> Result<Json<HashMap<String, AiConfig>>, AppError> {
let configs = state.app.storage.load_ai_provider_configs().await.map_err(AppError)?;
let configs = state.app.storage.load_ai_provider_configs().await.map_err(AppError::from)?;
Ok(Json(configs))
}
@@ -158,12 +158,12 @@ pub async fn save_ai_configs(
for item in &body.configs {
reject_web_unsupported_ai_provider(&item.config)?;
}
state.app.storage.save_ai_configs(&body.configs).await.map_err(AppError)?;
state.app.storage.save_ai_configs(&body.configs).await.map_err(AppError::from)?;
Ok(Json(()))
}
pub async fn load_ai_configs(State(state): State<Arc<WebState>>) -> Result<Json<Vec<AiConfigItem>>, AppError> {
let configs = state.app.storage.load_ai_configs().await.map_err(AppError)?;
let configs = state.app.storage.load_ai_configs().await.map_err(AppError::from)?;
Ok(Json(configs))
}
@@ -177,7 +177,7 @@ pub async fn set_default_ai_config(
State(state): State<Arc<WebState>>,
Json(body): Json<SetDefaultAiConfigRequest>,
) -> Result<Json<()>, AppError> {
state.app.storage.set_default_ai_config(&body.config_id).await.map_err(AppError)?;
state.app.storage.set_default_ai_config(&body.config_id).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -192,7 +192,7 @@ pub async fn save_ai_config_item(
Json(body): Json<SaveAiConfigItemRequest>,
) -> Result<Json<()>, AppError> {
reject_web_unsupported_ai_provider(&body.config.config)?;
state.app.storage.save_ai_config_item(&body.config).await.map_err(AppError)?;
state.app.storage.save_ai_config_item(&body.config).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -200,7 +200,7 @@ pub async fn delete_ai_config(
State(state): State<Arc<WebState>>,
Path(config_id): Path<String>,
) -> Result<Json<()>, AppError> {
state.app.storage.delete_ai_config(&config_id).await.map_err(AppError)?;
state.app.storage.delete_ai_config(&config_id).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -212,12 +212,12 @@ pub async fn save_ai_conversation(
State(state): State<Arc<WebState>>,
Json(body): Json<SaveAiConversationRequest>,
) -> Result<Json<()>, AppError> {
state.app.storage.save_ai_conversation(&body.conversation).await.map_err(AppError)?;
state.app.storage.save_ai_conversation(&body.conversation).await.map_err(AppError::from)?;
Ok(Json(()))
}
pub async fn load_ai_conversations(State(state): State<Arc<WebState>>) -> Result<Json<Vec<AiConversation>>, AppError> {
let conversations = state.app.storage.load_ai_conversations().await.map_err(AppError)?;
let conversations = state.app.storage.load_ai_conversations().await.map_err(AppError::from)?;
Ok(Json(conversations))
}
@@ -225,7 +225,7 @@ pub async fn delete_ai_conversation(
State(state): State<Arc<WebState>>,
Path(id): Path<String>,
) -> Result<Json<()>, AppError> {
state.app.storage.delete_ai_conversation(&id).await.map_err(AppError)?;
state.app.storage.delete_ai_conversation(&id).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -235,7 +235,7 @@ pub async fn delete_ai_conversation(
pub async fn ai_complete(Json(body): Json<AiCompleteRequest>) -> Result<Json<String>, AppError> {
reject_web_unsupported_ai_provider(&body.request.config)?;
let result = dbx_core::ai::complete(&body.request).await.map_err(AppError)?;
let result = dbx_core::ai::complete(&body.request).await.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -247,13 +247,13 @@ pub async fn ai_test_connection(
Json(body): Json<AiTestConnectionRequest>,
) -> Result<Json<AiTestConnectionResult>, AppError> {
reject_web_unsupported_ai_provider(&body.config)?;
let result = dbx_core::ai::test_connection_core(&body.config).await.map_err(AppError)?;
let result = dbx_core::ai::test_connection_core(&body.config).await.map_err(AppError::from)?;
Ok(Json(result))
}
pub async fn ai_list_models(Json(body): Json<AiListModelsRequest>) -> Result<Json<Vec<AiModelInfo>>, AppError> {
reject_web_unsupported_ai_provider(&body.config)?;
let result = dbx_core::ai::list_models_core(&body.config).await.map_err(AppError)?;
let result = dbx_core::ai::list_models_core(&body.config).await.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -323,7 +323,7 @@ pub async fn ai_agent_stream(
let (tx, rx) = tokio::sync::broadcast::channel::<String>(256);
let parsed_db_type: DatabaseType = serde_json::from_str(&format!("\"{}\"", body.db_type))
.map_err(|_| AppError(format!("Unknown database type: {}", body.db_type)))?;
.map_err(|_| AppError::from(format!("Unknown database type: {}", body.db_type)))?;
let production_database = state
.app
.configs
+5 -5
View File
@@ -39,7 +39,7 @@ pub struct DecryptConfigRequest {
}
pub async fn load_pinned_tree_node_ids(State(state): State<Arc<WebState>>) -> Result<Json<Vec<String>>, AppError> {
let ids = state.app.storage.load_pinned_tree_node_ids().await.map_err(AppError)?;
let ids = state.app.storage.load_pinned_tree_node_ids().await.map_err(AppError::from)?;
Ok(Json(ids))
}
@@ -47,26 +47,26 @@ pub async fn save_pinned_tree_node_ids(
State(state): State<Arc<WebState>>,
Json(body): Json<SavePinnedTreeNodeIdsRequest>,
) -> Result<Json<()>, AppError> {
state.app.storage.save_pinned_tree_node_ids(&body.ids).await.map_err(AppError)?;
state.app.storage.save_pinned_tree_node_ids(&body.ids).await.map_err(AppError::from)?;
Ok(Json(()))
}
pub async fn load_mcp_global_policy(
State(state): State<Arc<WebState>>,
) -> Result<Json<McpGlobalPolicyState>, AppError> {
state.app.storage.load_mcp_global_policy().await.map(Json).map_err(AppError)
state.app.storage.load_mcp_global_policy().await.map(Json).map_err(AppError::from)
}
pub async fn save_mcp_global_policy(
State(state): State<Arc<WebState>>,
Json(policy): Json<McpGlobalPolicy>,
) -> Result<Json<()>, AppError> {
state.app.storage.save_mcp_global_policy(&policy).await.map_err(AppError)?;
state.app.storage.save_mcp_global_policy(&policy).await.map_err(AppError::from)?;
Ok(Json(()))
}
pub async fn decrypt_config(Json(body): Json<DecryptConfigRequest>) -> Result<Json<String>, AppError> {
decrypt_config_payload(&body.payload, &body.passphrase).map(Json).map_err(AppError)
decrypt_config_payload(&body.payload, &body.passphrase).map(Json).map_err(AppError::from)
}
fn decrypt_config_payload(payload: &EncryptedConfigPayload, passphrase: &str) -> Result<String, String> {
+27 -27
View File
@@ -102,8 +102,8 @@ pub async fn webdav_sync_test(
State(state): State<Arc<WebState>>,
Json(mut req): Json<WebDavConfigRequest>,
) -> Result<Json<()>, AppError> {
resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError)?;
WebDavClient::new(req.config).test().await.map_err(AppError)?;
resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
WebDavClient::new(req.config).test().await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -111,14 +111,14 @@ pub async fn webdav_password_status(
State(state): State<Arc<WebState>>,
Json(req): Json<WebDavConfigRequest>,
) -> Result<Json<WebDavPasswordStatus>, AppError> {
webdav_saved_password_status(&state.app.storage, &req.config).await.map(Json).map_err(AppError)
webdav_saved_password_status(&state.app.storage, &req.config).await.map(Json).map_err(AppError::from)
}
pub async fn save_webdav_saved_password(
State(state): State<Arc<WebState>>,
Json(req): Json<SaveWebDavPasswordRequest>,
) -> Result<Json<()>, AppError> {
save_webdav_password(&state.app.storage, &req.config, &req.password).await.map_err(AppError)?;
save_webdav_password(&state.app.storage, &req.config, &req.password).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -126,14 +126,14 @@ pub async fn forget_webdav_saved_password(
State(state): State<Arc<WebState>>,
Json(req): Json<WebDavConfigRequest>,
) -> Result<Json<()>, AppError> {
forget_webdav_password(&state.app.storage, &req.config).await.map_err(AppError)?;
forget_webdav_password(&state.app.storage, &req.config).await.map_err(AppError::from)?;
Ok(Json(()))
}
pub async fn webdav_sync_secrets_status(
State(state): State<Arc<WebState>>,
) -> Result<Json<WebDavSyncSecretsStatus>, AppError> {
core_webdav_sync_secrets_status(&state.app.storage).await.map(Json).map_err(AppError)
core_webdav_sync_secrets_status(&state.app.storage).await.map(Json).map_err(AppError::from)
}
pub async fn save_webdav_sync_secrets_preference(
@@ -142,12 +142,12 @@ pub async fn save_webdav_sync_secrets_preference(
) -> Result<Json<()>, AppError> {
core_save_webdav_sync_secrets_preference(&state.app.storage, req.enabled, req.passphrase.as_deref())
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
pub async fn forget_webdav_sync_secrets_passphrase(State(state): State<Arc<WebState>>) -> Result<Json<()>, AppError> {
core_forget_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError)?;
core_forget_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -155,7 +155,7 @@ pub async fn webdav_sync_upload(
State(state): State<Arc<WebState>>,
Json(mut req): Json<WebDavUploadRequest>,
) -> Result<Json<WebDavSyncSummary>, AppError> {
resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError)?;
resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
let snapshot = build_sync_snapshot_with_saved_secrets(
&state.app.storage,
env!("CARGO_PKG_VERSION"),
@@ -163,21 +163,21 @@ pub async fn webdav_sync_upload(
req.secrets_passphrase.as_deref(),
)
.await
.map_err(AppError)?;
WebDavClient::new(req.config).put_snapshot(&snapshot).await.map(Json).map_err(AppError)
.map_err(AppError::from)?;
WebDavClient::new(req.config).put_snapshot(&snapshot).await.map(Json).map_err(AppError::from)
}
pub async fn webdav_sync_download(
State(state): State<Arc<WebState>>,
Json(mut req): Json<WebDavDownloadRequest>,
) -> Result<Json<WebDavDownloadResult>, AppError> {
resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError)?;
let (snapshot, summary) = WebDavClient::new(req.config).get_snapshot().await.map_err(AppError)?;
resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
let (snapshot, summary) = WebDavClient::new(req.config).get_snapshot().await.map_err(AppError::from)?;
let explicit_passphrase = req.secrets_passphrase.as_deref().map(str::trim).filter(|value| !value.is_empty());
let saved_passphrase = if explicit_passphrase.is_some() {
None
} else {
resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError)?
resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError::from)?
};
let apply_summary = apply_sync_snapshot(
&state.app.storage,
@@ -185,7 +185,7 @@ pub async fn webdav_sync_download(
ApplySnapshotOptions { secrets_passphrase: explicit_passphrase.or(saved_passphrase.as_deref()) },
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(WebDavDownloadResult {
summary,
editor_settings: snapshot.editor_settings,
@@ -198,8 +198,8 @@ pub async fn snippet_sync_test(
State(state): State<Arc<WebState>>,
Json(mut req): Json<SnippetConfigRequest>,
) -> Result<Json<()>, AppError> {
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError)?;
SnippetSyncClient::new(req.config).test().await.map_err(AppError)?;
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
SnippetSyncClient::new(req.config).test().await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -207,14 +207,14 @@ pub async fn snippet_token_status(
State(state): State<Arc<WebState>>,
Json(req): Json<SnippetConfigRequest>,
) -> Result<Json<SnippetTokenStatus>, AppError> {
snippet_saved_token_status(&state.app.storage, &req.config).await.map(Json).map_err(AppError)
snippet_saved_token_status(&state.app.storage, &req.config).await.map(Json).map_err(AppError::from)
}
pub async fn save_snippet_saved_token(
State(state): State<Arc<WebState>>,
Json(req): Json<SaveSnippetTokenRequest>,
) -> Result<Json<()>, AppError> {
save_snippet_token(&state.app.storage, &req.config, &req.token).await.map_err(AppError)?;
save_snippet_token(&state.app.storage, &req.config, &req.token).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -222,7 +222,7 @@ pub async fn forget_snippet_saved_token(
State(state): State<Arc<WebState>>,
Json(req): Json<SnippetConfigRequest>,
) -> Result<Json<()>, AppError> {
forget_snippet_token(&state.app.storage, &req.config).await.map_err(AppError)?;
forget_snippet_token(&state.app.storage, &req.config).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -230,7 +230,7 @@ pub async fn snippet_sync_upload(
State(state): State<Arc<WebState>>,
Json(mut req): Json<SnippetUploadRequest>,
) -> Result<Json<SnippetSyncSummary>, AppError> {
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError)?;
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
let snapshot = build_sync_snapshot_with_saved_secrets(
&state.app.storage,
env!("CARGO_PKG_VERSION"),
@@ -238,21 +238,21 @@ pub async fn snippet_sync_upload(
req.secrets_passphrase.as_deref(),
)
.await
.map_err(AppError)?;
SnippetSyncClient::new(req.config).put_snapshot(&snapshot).await.map(Json).map_err(AppError)
.map_err(AppError::from)?;
SnippetSyncClient::new(req.config).put_snapshot(&snapshot).await.map(Json).map_err(AppError::from)
}
pub async fn snippet_sync_download(
State(state): State<Arc<WebState>>,
Json(mut req): Json<SnippetDownloadRequest>,
) -> Result<Json<SnippetDownloadResult>, AppError> {
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError)?;
let (snapshot, summary) = SnippetSyncClient::new(req.config).get_snapshot().await.map_err(AppError)?;
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
let (snapshot, summary) = SnippetSyncClient::new(req.config).get_snapshot().await.map_err(AppError::from)?;
let explicit_passphrase = req.secrets_passphrase.as_deref().map(str::trim).filter(|value| !value.is_empty());
let saved_passphrase = if explicit_passphrase.is_some() {
None
} else {
resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError)?
resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError::from)?
};
let apply_summary = apply_sync_snapshot(
&state.app.storage,
@@ -260,7 +260,7 @@ pub async fn snippet_sync_download(
ApplySnapshotOptions { secrets_passphrase: explicit_passphrase.or(saved_passphrase.as_deref()) },
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(SnippetDownloadResult {
summary,
editor_settings: snapshot.editor_settings,
+29 -24
View File
@@ -111,14 +111,14 @@ pub async fn test_connection(
run_temporary_connection_test(&state.app, body.config, false)
.await
.map(|result| Json(result.message))
.map_err(AppError)
.map_err(AppError::from)
}
pub async fn test_connection_with_info(
State(state): State<Arc<WebState>>,
Json(body): Json<ConnectRequest>,
) -> Result<Json<ConnectionTestResult>, AppError> {
run_temporary_connection_test(&state.app, body.config, true).await.map(Json).map_err(AppError)
run_temporary_connection_test(&state.app, body.config, true).await.map(Json).map_err(AppError::from)
}
pub async fn connect_db(
@@ -132,7 +132,7 @@ pub async fn connect_db(
&config.password,
!config.attached_databases.is_empty(),
)
.map_err(AppError)?;
.map_err(AppError::from)?;
}
let app = &state.app;
let connection_id = config.id.clone();
@@ -142,7 +142,7 @@ pub async fn connect_db(
app.reset_connection_transport_for_config(&connection_id, &config).await;
app.configs.write().await.insert(connection_id.clone(), config.clone());
app.get_or_create_pool_for_connection_attempt(&connection_id, None, attempt).await.map_err(AppError)?;
app.get_or_create_pool_for_connection_attempt(&connection_id, None, attempt).await.map_err(AppError::from)?;
Ok(Json(connection_id))
}
@@ -151,7 +151,12 @@ pub async fn connected_database_info(
State(state): State<Arc<WebState>>,
Json(body): Json<ConnectionIdentifierQuoteRequest>,
) -> Result<Json<Option<DatabaseConnectionInfo>>, AppError> {
state.app.connection_database_info(&body.connection_id, body.database.as_deref()).await.map(Json).map_err(AppError)
state
.app
.connection_database_info(&body.connection_id, body.database.as_deref())
.await
.map(Json)
.map_err(AppError::from)
}
pub async fn save_connection_database_info(
@@ -163,7 +168,7 @@ pub async fn save_connection_database_info(
.save_connection_database_info(&body.connection_id, body.database_info)
.await
.map(|_| Json(()))
.map_err(AppError)
.map_err(AppError::from)
}
pub async fn connection_final_proxy_port(
@@ -172,7 +177,7 @@ pub async fn connection_final_proxy_port(
) -> Result<Json<u16>, AppError> {
let runtime_config = body.config.canonicalized();
if !runtime_config.has_effective_transport_layers() {
return Err(AppError("Connection has no configured transport layers".to_string()));
return Err(AppError::from("Connection has no configured transport layers".to_string()));
}
if runtime_config.db_type == dbx_core::models::connection::DatabaseType::Sqlite {
dbx_core::db::sqlite::validate_persistent_attachments(
@@ -180,7 +185,7 @@ pub async fn connection_final_proxy_port(
&runtime_config.password,
!runtime_config.attached_databases.is_empty(),
)
.map_err(AppError)?;
.map_err(AppError::from)?;
}
let app = &state.app;
@@ -188,7 +193,7 @@ pub async fn connection_final_proxy_port(
let db_config = dbx_core::connection::metadata_connection_config(&runtime_config);
app.configs.write().await.insert(connection_id.clone(), runtime_config);
let (_, port) = app.connection_host_port(&connection_id, &db_config).await.map_err(AppError)?;
let (_, port) = app.connection_host_port(&connection_id, &db_config).await.map_err(AppError::from)?;
Ok(Json(port))
}
@@ -224,7 +229,7 @@ pub async fn check_connection_health(
State(state): State<Arc<WebState>>,
Json(body): Json<DisconnectRequest>,
) -> Result<Json<()>, AppError> {
state.app.check_connection_health(&body.connection_id).await.map_err(AppError)?;
state.app.check_connection_health(&body.connection_id).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -237,7 +242,7 @@ pub async fn connection_identifier_quote(
.connection_identifier_quote(&body.connection_id, body.database.as_deref())
.await
.map(Json)
.map_err(AppError)
.map_err(AppError::from)
}
pub async fn close_database_connection(
@@ -246,7 +251,7 @@ pub async fn close_database_connection(
) -> Result<Json<bool>, AppError> {
let database = body.database.trim();
let database = if database.is_empty() { None } else { Some(database) };
state.app.close_database_pool(&body.connection_id, database).await.map(Json).map_err(AppError)
state.app.close_database_pool(&body.connection_id, database).await.map(Json).map_err(AppError::from)
}
pub async fn save_connections(
@@ -260,10 +265,10 @@ pub async fn save_connections(
&config.password,
!config.attached_databases.is_empty(),
)
.map_err(AppError)?;
.map_err(AppError::from)?;
}
}
state.app.storage.save_connections(&body.configs).await.map_err(AppError)?;
state.app.storage.save_connections(&body.configs).await.map_err(AppError::from)?;
let sync = sync_connection_configs(&state, &body.configs).await;
remove_connection_pools_for_connection_ids(&state, &sync.connection_pool_ids_to_drop).await;
drop_nacos_adapters_for_connection_ids(&state, &sync.nacos_adapter_ids_to_drop).await;
@@ -275,7 +280,7 @@ pub async fn mcp_add_connection(
State(state): State<Arc<WebState>>,
Json(body): Json<McpAddConnectionRequest>,
) -> Result<Json<ConnectionConfig>, AppError> {
let saved = state.app.storage.add_connection_for_mcp(body.config).await.map_err(AppError)?;
let saved = state.app.storage.add_connection_for_mcp(body.config).await.map_err(AppError::from)?;
state.app.configs.write().await.insert(saved.id.clone(), saved.clone());
Ok(Json(saved))
}
@@ -285,7 +290,7 @@ pub async fn mcp_remove_connection(
Json(body): Json<McpRemoveConnectionRequest>,
) -> Result<Json<bool>, AppError> {
let connection_id = body.connection_id;
let removed = state.app.storage.remove_connection_for_mcp(&connection_id).await.map_err(AppError)?;
let removed = state.app.storage.remove_connection_for_mcp(&connection_id).await.map_err(AppError::from)?;
if removed {
state.app.configs.write().await.remove(&connection_id);
state.app.remove_connection_pools_detached(&connection_id).await;
@@ -297,7 +302,7 @@ pub async fn mcp_remove_connection(
}
pub async fn load_connections(State(state): State<Arc<WebState>>) -> Result<Json<Vec<ConnectionConfig>>, AppError> {
let configs = state.app.storage.load_connections().await.map_err(AppError)?;
let configs = state.app.storage.load_connections().await.map_err(AppError::from)?;
let sync = sync_connection_configs(&state, &configs).await;
remove_connection_pools_for_connection_ids(&state, &sync.connection_pool_ids_to_drop).await;
drop_nacos_adapters_for_connection_ids(&state, &sync.nacos_adapter_ids_to_drop).await;
@@ -507,13 +512,13 @@ mod tests {
Json(ConnectRequest { config: config.clone(), client_attempt: None }),
)
.await
.unwrap_or_else(|error| panic!("{}", error.0));
.unwrap_or_else(|error| panic!("{}", error.message));
assert_eq!(legacy.0, "Connection successful");
let detailed =
test_connection_with_info(State(state.clone()), Json(ConnectRequest { config, client_attempt: None }))
.await
.unwrap_or_else(|error| panic!("{}", error.0));
.unwrap_or_else(|error| panic!("{}", error.message));
assert_eq!(detailed.0.message, "Connection successful");
assert_eq!(detailed.0.database_info, None);
assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_")));
@@ -545,7 +550,7 @@ mod tests {
connect_db(State(state.clone()), Json(ConnectRequest { config: invalid.clone(), client_attempt: None }))
.await
.unwrap_err();
assert!(connect_error.0.contains("in-memory main database"), "{}", connect_error.0);
assert!(connect_error.message.contains("in-memory main database"), "{}", connect_error.message);
invalid.transport_layers.push(TransportLayerConfig::Proxy(ProxyTunnelConfig {
id: "proxy".to_string(),
@@ -565,7 +570,7 @@ mod tests {
)
.await
.unwrap_err();
assert!(proxy_error.0.contains("in-memory main database"), "{}", proxy_error.0);
assert!(proxy_error.message.contains("in-memory main database"), "{}", proxy_error.message);
assert!(state.app.connections.read().await.contains_key(&initial.id));
assert_eq!(state.app.configs.read().await.get(&initial.id), Some(&initial));
@@ -681,7 +686,7 @@ mod tests {
Json(McpRemoveConnectionRequest { connection_id: removed.id.clone() }),
)
.await
.unwrap_or_else(|error| panic!("{}", error.0));
.unwrap_or_else(|error| panic!("{}", error.message));
assert!(removed_result.0);
assert_eq!(state.app.storage.load_connections().await.unwrap()[0].id, kept.id);
@@ -691,7 +696,7 @@ mod tests {
)
.await
.unwrap_err();
assert!(scope_error.0.starts_with("CONNECTION_OUT_OF_SCOPE:"));
assert!(scope_error.message.starts_with("CONNECTION_OUT_OF_SCOPE:"));
state
.app
@@ -709,7 +714,7 @@ mod tests {
)
.await
.unwrap_err();
assert!(read_only_error.0.starts_with("MCP_READ_ONLY:"));
assert!(read_only_error.message.starts_with("MCP_READ_ONLY:"));
let _ = std::fs::remove_dir_all(dir);
}
+9 -3
View File
@@ -8,21 +8,27 @@ use crate::state::WebState;
pub async fn prepare_data_compare(
Json(options): Json<dbx_core::data_compare::DataComparePreparationOptions>,
) -> Result<Json<dbx_core::data_compare::DataComparePreparation>, AppError> {
dbx_core::data_compare::prepare_data_compare(options).map(Json).map_err(AppError)
dbx_core::data_compare::prepare_data_compare(options).map(Json).map_err(AppError::from)
}
pub async fn prepare_data_compare_from_tables(
State(state): State<Arc<WebState>>,
Json(options): Json<dbx_core::data_compare::DataCompareFromTablesOptions>,
) -> Result<Json<dbx_core::data_compare::DataCompareFromTablesPreparation>, AppError> {
dbx_core::data_compare::prepare_data_compare_from_tables(&state.app, options).await.map(Json).map_err(AppError)
dbx_core::data_compare::prepare_data_compare_from_tables(&state.app, options)
.await
.map(Json)
.map_err(AppError::from)
}
pub async fn prepare_data_compare_missing_target(
State(state): State<Arc<WebState>>,
Json(options): Json<dbx_core::data_compare::DataCompareMissingTargetOptions>,
) -> Result<Json<dbx_core::data_compare::DataCompareFromTablesPreparation>, AppError> {
dbx_core::data_compare::prepare_data_compare_missing_target(&state.app, options).await.map(Json).map_err(AppError)
dbx_core::data_compare::prepare_data_compare_missing_target(&state.app, options)
.await
.map(Json)
.map_err(AppError::from)
}
pub async fn build_data_compare_sync_plan(
+5 -5
View File
@@ -40,7 +40,7 @@ pub async fn start_database_export(
// CWD, unreachable by the browser). The file is served back via the download
// endpoint after the export completes.
let tmp_dir = state.data_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?;
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
let tmp_file = tmp_dir.join(format!("database_export_{export_id}.sql"));
let tmp_file_path = tmp_file.to_string_lossy().to_string();
req.file_path = tmp_file_path.clone();
@@ -128,7 +128,7 @@ pub async fn database_export_progress(
Path(export_id): Path<String>,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> {
let channels = state.sse_channels.read().await;
let tx = channels.get(&export_id).ok_or_else(|| AppError("Export not found".to_string()))?;
let tx = channels.get(&export_id).ok_or_else(|| AppError::from("Export not found".to_string()))?;
let rx = tx.subscribe();
drop(channels);
Ok(crate::sse::sse_from_channel(rx))
@@ -151,9 +151,9 @@ pub async fn database_export_download(
.write()
.await
.remove(&export_id)
.ok_or_else(|| AppError("Export file not found".to_string()))?;
.ok_or_else(|| AppError::from("Export file not found".to_string()))?;
let data = tokio::fs::read(&file_path).await.map_err(|e| AppError(e.to_string()))?;
let data = tokio::fs::read(&file_path).await.map_err(|e| AppError::from(e.to_string()))?;
// Clean up temp file
let _ = tokio::fs::remove_file(&file_path).await;
@@ -162,5 +162,5 @@ pub async fn database_export_download(
.header(header::CONTENT_TYPE, "application/sql; charset=utf-8")
.header(header::CONTENT_DISPOSITION, format!("attachment; filename=\"{download_filename}\""))
.body(Body::from(data))
.map_err(|e| AppError(e.to_string()))
.map_err(|e| AppError::from(e.to_string()))
}
+29 -28
View File
@@ -20,11 +20,11 @@ where
let token = query.token();
tokio::select! {
biased;
_ = token.cancelled() => Err(AppError(dbx_core::query::canceled_error())),
result = future => result.map_err(AppError),
_ = token.cancelled() => Err(AppError::from(dbx_core::query::canceled_error())),
result = future => result.map_err(AppError::from),
}
} else {
future.await.map_err(AppError)
future.await.map_err(AppError::from)
}
}
@@ -34,7 +34,7 @@ async fn ensure_writable(
action: &str,
) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
return Err(AppError(format!(
return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
name, action
)));
@@ -149,7 +149,8 @@ pub async fn list_databases(
State(state): State<Arc<WebState>>,
Json(req): Json<DocumentListDatabasesRequest>,
) -> Result<Json<Vec<String>>, AppError> {
let result = dbx_core::document_ops::list_databases_core(&state.app, &req.connection_id).await.map_err(AppError)?;
let result =
dbx_core::document_ops::list_databases_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -159,7 +160,7 @@ pub async fn list_collections(
) -> Result<Json<Vec<dbx_core::document_ops::CollectionInfo>>, AppError> {
let result = dbx_core::document_ops::list_collections_core(&state.app, &req.connection_id, &req.database)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -200,7 +201,7 @@ pub async fn insert_document(
req.routing.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -219,7 +220,7 @@ pub async fn update_document(
req.routing.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -237,7 +238,7 @@ pub async fn delete_document(
req.routing.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -254,7 +255,7 @@ pub async fn list_gridfs_files(
req.sort.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -270,7 +271,7 @@ pub async fn list_gridfs_buckets(
req.sort.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -281,7 +282,7 @@ pub async fn create_gridfs_bucket(
ensure_writable(&state.app, &req.connection_id, "Create GridFS bucket").await?;
dbx_core::document_ops::create_gridfs_bucket_core(&state.app, &req.connection_id, &req.database, &req.bucket)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -292,7 +293,7 @@ pub async fn delete_gridfs_bucket(
ensure_writable(&state.app, &req.connection_id, "Delete GridFS bucket").await?;
dbx_core::document_ops::delete_gridfs_bucket_core(&state.app, &req.connection_id, &req.database, &req.bucket)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -308,7 +309,7 @@ pub async fn download_gridfs_file(
&req.file_id,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -323,14 +324,14 @@ pub async fn upload_gridfs_file(
let mut content_type: Option<String> = None;
let mut file_bytes: Option<Vec<u8>> = None;
while let Some(field) = multipart.next_field().await.map_err(|e| AppError(e.to_string()))? {
while let Some(field) = multipart.next_field().await.map_err(|e| AppError::from(e.to_string()))? {
let name = field.name().unwrap_or_default().to_string();
match name.as_str() {
"connectionId" => connection_id = Some(field.text().await.map_err(|e| AppError(e.to_string()))?),
"database" => database = Some(field.text().await.map_err(|e| AppError(e.to_string()))?),
"bucket" => bucket = Some(field.text().await.map_err(|e| AppError(e.to_string()))?),
"fileName" => file_name = Some(field.text().await.map_err(|e| AppError(e.to_string()))?),
"contentType" => content_type = Some(field.text().await.map_err(|e| AppError(e.to_string()))?),
"connectionId" => connection_id = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?),
"database" => database = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?),
"bucket" => bucket = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?),
"fileName" => file_name = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?),
"contentType" => content_type = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?),
"file" => {
if file_name.is_none() {
file_name = field.file_name().map(str::to_string);
@@ -338,7 +339,7 @@ pub async fn upload_gridfs_file(
if content_type.is_none() {
content_type = field.content_type().map(str::to_string);
}
file_bytes = Some(field.bytes().await.map_err(|e| AppError(e.to_string()))?.to_vec());
file_bytes = Some(field.bytes().await.map_err(|e| AppError::from(e.to_string()))?.to_vec());
}
_ => {
let _ = field.bytes().await;
@@ -346,11 +347,11 @@ pub async fn upload_gridfs_file(
}
}
let connection_id = connection_id.ok_or_else(|| AppError("Missing connectionId".to_string()))?;
let database = database.ok_or_else(|| AppError("Missing database".to_string()))?;
let bucket = bucket.ok_or_else(|| AppError("Missing bucket".to_string()))?;
let file_name = file_name.ok_or_else(|| AppError("Missing fileName".to_string()))?;
let file_bytes = file_bytes.ok_or_else(|| AppError("No file uploaded".to_string()))?;
let connection_id = connection_id.ok_or_else(|| AppError::from("Missing connectionId".to_string()))?;
let database = database.ok_or_else(|| AppError::from("Missing database".to_string()))?;
let bucket = bucket.ok_or_else(|| AppError::from("Missing bucket".to_string()))?;
let file_name = file_name.ok_or_else(|| AppError::from("Missing fileName".to_string()))?;
let file_bytes = file_bytes.ok_or_else(|| AppError::from("No file uploaded".to_string()))?;
ensure_writable(&state.app, &connection_id, "Upload GridFS file").await?;
let result = dbx_core::document_ops::upload_gridfs_file_core(
@@ -363,7 +364,7 @@ pub async fn upload_gridfs_file(
content_type.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -380,6 +381,6 @@ pub async fn delete_gridfs_file(
&req.file_id,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
+6 -5
View File
@@ -14,7 +14,7 @@ async fn ensure_writable(
action: &str,
) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
return Err(AppError(format!(
return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
name, action
)));
@@ -59,7 +59,7 @@ pub async fn list_prefix(
req.continuation.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -67,7 +67,8 @@ pub async fn get(
State(state): State<Arc<WebState>>,
Json(req): Json<EtcdKeyRequest>,
) -> Result<Json<dbx_core::agent_kv::KvGetResponse>, AppError> {
let result = dbx_core::agent_kv::kv_get_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError)?;
let result =
dbx_core::agent_kv::kv_get_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -78,7 +79,7 @@ pub async fn put(
ensure_writable(&state.app, &req.connection_id, "Put").await?;
let result = dbx_core::agent_kv::kv_put_core(&state.app, &req.connection_id, &req.key, req.value, req.lease)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -88,6 +89,6 @@ pub async fn delete(
) -> Result<Json<dbx_core::agent_kv::KvDeleteResponse>, AppError> {
ensure_writable(&state.app, &req.connection_id, "Delete").await?;
let result =
dbx_core::agent_kv::kv_delete_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError)?;
dbx_core::agent_kv::kv_delete_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError::from)?;
Ok(Json(result))
}
+7 -6
View File
@@ -25,7 +25,7 @@ pub async fn save_history(
State(state): State<Arc<WebState>>,
Json(body): Json<SaveHistoryRequest>,
) -> Result<Json<()>, AppError> {
state.app.storage.save_history_entry(&body.entry).await.map_err(AppError)?;
state.app.storage.save_history_entry(&body.entry).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -35,7 +35,8 @@ pub async fn load_history(
) -> Result<Json<Vec<HistoryEntry>>, AppError> {
let limit = q.limit.unwrap_or(100);
let offset = q.offset.unwrap_or(0);
let entries = state.app.storage.load_history_entries(limit, offset, q.activity_kind).await.map_err(AppError)?;
let entries =
state.app.storage.load_history_entries(limit, offset, q.activity_kind).await.map_err(AppError::from)?;
Ok(Json(entries))
}
@@ -43,17 +44,17 @@ pub async fn search_history(
State(state): State<Arc<WebState>>,
Json(request): Json<HistorySearchRequest>,
) -> Result<Json<HistorySearchResult>, AppError> {
state.app.storage.search_history_entries(request).await.map(Json).map_err(AppError)
state.app.storage.search_history_entries(request).await.map(Json).map_err(AppError::from)
}
pub async fn load_history_connection_options(
State(state): State<Arc<WebState>>,
) -> Result<Json<Vec<HistoryConnectionOption>>, AppError> {
state.app.storage.load_history_connection_options().await.map(Json).map_err(AppError)
state.app.storage.load_history_connection_options().await.map(Json).map_err(AppError::from)
}
pub async fn clear_history(State(state): State<Arc<WebState>>) -> Result<Json<()>, AppError> {
state.app.storage.clear_history().await.map_err(AppError)?;
state.app.storage.clear_history().await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -61,6 +62,6 @@ pub async fn delete_history_entry(
State(state): State<Arc<WebState>>,
Path(id): Path<String>,
) -> Result<Json<()>, AppError> {
state.app.storage.delete_history_entry(&id).await.map_err(AppError)?;
state.app.storage.delete_history_entry(&id).await.map_err(AppError::from)?;
Ok(Json(()))
}
+2 -2
View File
@@ -17,11 +17,11 @@ pub async fn save_sidebar_layout(
State(state): State<Arc<WebState>>,
Json(body): Json<SaveLayoutRequest>,
) -> Result<Json<()>, AppError> {
state.app.storage.save_sidebar_layout(&body.layout).await.map_err(AppError)?;
state.app.storage.save_sidebar_layout(&body.layout).await.map_err(AppError::from)?;
Ok(Json(()))
}
pub async fn load_sidebar_layout(State(state): State<Arc<WebState>>) -> Result<Json<serde_json::Value>, AppError> {
let layout = state.app.storage.load_sidebar_layout().await.map_err(AppError)?;
let layout = state.app.storage.load_sidebar_layout().await.map_err(AppError::from)?;
Ok(Json(layout.unwrap_or(serde_json::json!(null))))
}
+18 -14
View File
@@ -228,12 +228,12 @@ fn mongo_field_predicate_is_exists_true(value: &serde_json::Value) -> bool {
}
async fn load_policy(state: &Arc<WebState>) -> Result<McpGlobalPolicy, AppError> {
state.app.storage.load_mcp_global_policy().await.map(|state| state.policy()).map_err(AppError)
state.app.storage.load_mcp_global_policy().await.map(|state| state.policy()).map_err(AppError::from)
}
fn ensure_allowed(policy: &McpGlobalPolicy, connection_id: &str) -> Result<(), AppError> {
if policy.allowed_connection_ids.as_ref().is_some_and(|allowed| !allowed.iter().any(|id| id == connection_id)) {
return Err(AppError(format!(
return Err(AppError::from(format!(
"CONNECTION_OUT_OF_SCOPE: connection '{connection_id}' is not allowed by DBX MCP settings"
)));
}
@@ -241,7 +241,7 @@ fn ensure_allowed(policy: &McpGlobalPolicy, connection_id: &str) -> Result<(), A
}
fn connection_read_only_error(message: impl Into<String>) -> AppError {
AppError(format!("CONNECTION_READ_ONLY: {}", message.into()))
AppError::from(format!("CONNECTION_READ_ONLY: {}", message.into()))
}
async fn load_connection(state: &Arc<WebState>, connection_id: &str) -> Result<ConnectionConfig, AppError> {
@@ -250,10 +250,10 @@ async fn load_connection(state: &Arc<WebState>, connection_id: &str) -> Result<C
.storage
.load_connections()
.await
.map_err(AppError)?
.map_err(AppError::from)?
.into_iter()
.find(|config| config.id == connection_id)
.ok_or_else(|| AppError(format!("Connection with id '{connection_id}' not found")))
.ok_or_else(|| AppError::from(format!("Connection with id '{connection_id}' not found")))
}
pub async fn ensure_scope(state: &Arc<WebState>, headers: &HeaderMap, connection_id: &str) -> Result<(), AppError> {
@@ -295,7 +295,7 @@ pub async fn ensure_mongo_pipeline_target(
}
let config = load_connection(state, connection_id).await?;
if dbx_core::production_safety::mongo_pipeline_targets_production_database(&config, database, pipeline_json) {
return Err(AppError(
return Err(AppError::from(
"PRODUCTION_DATABASE_READ_ONLY: MongoDB aggregate write targeting production scope is blocked.".to_string(),
));
}
@@ -316,10 +316,12 @@ async fn ensure_write_with_risk(
let policy = load_policy(state).await?;
ensure_allowed(&policy, connection_id)?;
if policy.read_only {
return Err(AppError(format!("MCP_READ_ONLY: DBX MCP read-only mode is enabled. {action} blocked.")));
return Err(AppError::from(format!("MCP_READ_ONLY: DBX MCP read-only mode is enabled. {action} blocked.")));
}
if dangerous && !policy.allow_dangerous_sql {
return Err(AppError(format!("SQL_BLOCKED: High-risk operation '{action}' is disabled in DBX MCP settings.")));
return Err(AppError::from(format!(
"SQL_BLOCKED: High-risk operation '{action}' is disabled in DBX MCP settings."
)));
}
let config = load_connection(state, connection_id).await?;
if config.read_only {
@@ -329,7 +331,7 @@ async fn ensure_write_with_risk(
)));
}
if dbx_core::production_safety::is_production_database(&config, database) {
return Err(AppError(format!(
return Err(AppError::from(format!(
"PRODUCTION_DATABASE_READ_ONLY: {action} blocked for production database '{database}'."
)));
}
@@ -350,21 +352,23 @@ pub async fn ensure_sql(
ensure_allowed(&policy, connection_id)?;
let config = load_connection(state, connection_id).await?;
if dbx_core::sql_risk::mcp_sql_has_forbidden_database_switch(sql, config.db_type) {
return Err(AppError("SQL_BLOCKED: MCP does not allow USE or persistent database switching.".to_string()));
return Err(AppError::from(
"SQL_BLOCKED: MCP does not allow USE or persistent database switching.".to_string(),
));
}
let is_write = dbx_core::query_execution_sql::is_write_sql_for_database(sql, config.db_type);
if policy.read_only && is_write {
return Err(AppError("MCP_READ_ONLY: DBX MCP read-only mode is enabled. SQL write blocked.".to_string()));
return Err(AppError::from("MCP_READ_ONLY: DBX MCP read-only mode is enabled. SQL write blocked.".to_string()));
}
if !policy.allow_dangerous_sql && dbx_core::sql_risk::is_dangerous_sql_for_database(sql, config.db_type) {
return Err(AppError("SQL_BLOCKED: High-risk SQL is disabled in DBX MCP settings.".to_string()));
return Err(AppError::from("SQL_BLOCKED: High-risk SQL is disabled in DBX MCP settings.".to_string()));
}
if config.read_only {
dbx_core::query_execution_sql::check_read_only(sql, &config.name, config.db_type)
.map_err(connection_read_only_error)?;
}
if is_write && dbx_core::production_safety::targets_production_database(&config, database, sql) {
return Err(AppError(
return Err(AppError::from(
"PRODUCTION_DATABASE_READ_ONLY: SQL write targeting production scope is blocked.".to_string(),
));
}
@@ -377,7 +381,7 @@ mod tests {
#[test]
fn connection_read_only_errors_use_the_stable_mcp_code() {
assert_eq!(connection_read_only_error("write blocked").0, "CONNECTION_READ_ONLY: write blocked");
assert_eq!(connection_read_only_error("write blocked").message, "CONNECTION_READ_ONLY: write blocked");
}
#[test]
+1
View File
@@ -16,6 +16,7 @@ pub mod mongo;
pub mod mq;
pub mod nacos;
pub mod plugins;
pub mod prompt_template;
pub mod query;
pub mod query_result_export;
pub mod redis;
+37 -37
View File
@@ -18,7 +18,7 @@ pub struct MongoParseShellCommandRequest {
pub async fn parse_shell_command(
Json(req): Json<MongoParseShellCommandRequest>,
) -> Result<Json<dbx_core::mongo_shell::MongoCommand>, AppError> {
dbx_core::mongo_shell::parse(&req.source).map(Json).map_err(AppError)
dbx_core::mongo_shell::parse(&req.source).map(Json).map_err(AppError::from)
}
async fn run_cancellable<T, F>(state: &Arc<WebState>, execution_id: Option<String>, future: F) -> Result<T, AppError>
@@ -33,11 +33,11 @@ where
let token = query.token();
tokio::select! {
biased;
_ = token.cancelled() => Err(AppError(dbx_core::query::canceled_error())),
result = future => result.map_err(AppError),
_ = token.cancelled() => Err(AppError::from(dbx_core::query::canceled_error())),
result = future => result.map_err(AppError::from),
}
} else {
future.await.map_err(AppError)
future.await.map_err(AppError::from)
}
}
@@ -48,7 +48,7 @@ async fn ensure_writable(
action: &str,
) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
return Err(AppError(format!(
return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
name, action
)));
@@ -284,7 +284,7 @@ pub async fn list_databases(
) -> Result<Json<Vec<String>>, AppError> {
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
let result =
dbx_core::mongo_ops::mongo_list_databases_core(&state.app, &req.connection_id).await.map_err(AppError)?;
dbx_core::mongo_ops::mongo_list_databases_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -296,7 +296,7 @@ pub async fn list_collections(
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
let result = dbx_core::mongo_ops::mongo_list_collections_core(&state.app, &req.connection_id, &req.database)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -319,7 +319,7 @@ pub async fn vector_collection_detail(
&req.collection,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -330,7 +330,7 @@ pub async fn create_database(
ensure_writable(&state.app, &req.connection_id, "Create database").await?;
dbx_core::mongo_ops::mongo_create_database_core(&state.app, &req.connection_id, &req.database)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
@@ -341,7 +341,7 @@ pub async fn drop_database(
ensure_writable(&state.app, &req.connection_id, "Drop database").await?;
dbx_core::mongo_ops::mongo_drop_database_core(&state.app, &req.connection_id, &req.database)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
@@ -355,7 +355,7 @@ pub async fn drop_collection(
ensure_writable(&state.app, &req.connection_id, "Drop collection").await?;
dbx_core::mongo_ops::mongo_drop_collection_core(&state.app, &req.connection_id, &req.database, &req.collection)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
@@ -375,7 +375,7 @@ pub async fn rename_collection(
&req.new_name,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true })))
}
@@ -401,7 +401,7 @@ pub async fn find_documents(
),
)
.await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn find_one(
@@ -422,7 +422,7 @@ pub async fn find_one(
),
)
.await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn count_documents(
@@ -459,7 +459,7 @@ pub async fn server_version(
dbx_core::mongo_ops::mongo_server_version_core(&state.app, &req.connection_id, &req.database),
)
.await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn collection_stats(
@@ -480,7 +480,7 @@ pub async fn collection_stats(
),
)
.await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn aggregate_documents(
@@ -521,7 +521,7 @@ pub async fn aggregate_documents(
),
)
.await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn distinct(
@@ -543,7 +543,7 @@ pub async fn distinct(
),
)
.await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn create_index(
@@ -563,7 +563,7 @@ pub async fn create_index(
req.options_json.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "name": name })))
}
@@ -584,8 +584,8 @@ pub async fn drop_indexes(
req.single,
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn insert_document(
@@ -602,7 +602,7 @@ pub async fn insert_document(
None,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -621,7 +621,7 @@ pub async fn insert_documents(
&req.docs_json,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "affected_rows": result })))
}
@@ -640,7 +640,7 @@ pub async fn update_document(
req.routing.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -667,7 +667,7 @@ pub async fn update_documents(
req.options_json.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "affected_rows": result })))
}
@@ -689,8 +689,8 @@ pub async fn find_one_and_update(
req.options_json.as_deref(),
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn find_one_and_replace(
@@ -711,8 +711,8 @@ pub async fn find_one_and_replace(
req.options_json.as_deref(),
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn find_one_and_delete(
@@ -732,8 +732,8 @@ pub async fn find_one_and_delete(
req.options_json.as_deref(),
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn delete_document(
@@ -750,7 +750,7 @@ pub async fn delete_document(
req.routing.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -775,7 +775,7 @@ pub async fn delete_documents(
req.many,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "affected_rows": result })))
}
@@ -873,12 +873,12 @@ mod tests {
let revoked = ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Update")
.await
.unwrap_err();
assert!(revoked.0.starts_with("MCP_READ_ONLY:"), "{}", revoked.0);
assert!(revoked.message.starts_with("MCP_READ_ONLY:"), "{}", revoked.message);
state.app.storage.save_mcp_global_policy(&writable_policy).await.unwrap();
let empty_filter =
ensure_find_one_write_policy(&state, &headers, &connection.id, "app", "{}", "Delete").await.unwrap_err();
assert!(empty_filter.0.starts_with("SQL_BLOCKED:"), "{}", empty_filter.0);
assert!(empty_filter.message.starts_with("SQL_BLOCKED:"), "{}", empty_filter.message);
state
.app
@@ -893,7 +893,7 @@ mod tests {
ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Update")
.await
.unwrap_err();
assert!(production.0.starts_with("PRODUCTION_DATABASE_READ_ONLY:"), "{}", production.0);
assert!(production.message.starts_with("PRODUCTION_DATABASE_READ_ONLY:"), "{}", production.message);
state.app.storage.save_connections(std::slice::from_ref(&connection)).await.unwrap();
state
@@ -909,7 +909,7 @@ mod tests {
let allowlist = ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Delete")
.await
.unwrap_err();
assert!(allowlist.0.starts_with("CONNECTION_OUT_OF_SCOPE:"), "{}", allowlist.0);
assert!(allowlist.message.starts_with("CONNECTION_OUT_OF_SCOPE:"), "{}", allowlist.message);
drop(state);
let _ = std::fs::remove_dir_all(dir);
+65 -57
View File
@@ -298,7 +298,7 @@ pub(crate) struct RawRequestReq {
async fn ensure_writable(app: &dbx_core::connection::AppState, conn_id: &str, action: &str) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, conn_id).await {
return Err(AppError(format!("Read-only connection '{name}'. {action} is blocked.")));
return Err(AppError::from(format!("Read-only connection '{name}'. {action} is blocked.")));
}
Ok(())
}
@@ -310,7 +310,7 @@ pub async fn test_connection(
Json(req): Json<ConnReq>,
) -> Result<Json<dbx_core::mq::MqClusterInfo>, AppError> {
let result =
dbx_core::mq::service::mq_test_connection_core(&state.app, &req.connection_id).await.map_err(AppError)?;
dbx_core::mq::service::mq_test_connection_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -318,7 +318,8 @@ pub async fn list_tenants(
State(state): State<Arc<WebState>>,
Json(req): Json<ConnReq>,
) -> Result<Json<Vec<dbx_core::mq::TenantInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_tenants_core(&state.app, &req.connection_id).await.map_err(AppError)?;
let result =
dbx_core::mq::service::mq_list_tenants_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -326,8 +327,9 @@ pub async fn get_tenant(
State(state): State<Arc<WebState>>,
Json(req): Json<TenantReq>,
) -> Result<Json<dbx_core::mq::TenantInfo>, AppError> {
let result =
dbx_core::mq::service::mq_get_tenant_core(&state.app, &req.connection_id, &req.name).await.map_err(AppError)?;
let result = dbx_core::mq::service::mq_get_tenant_core(&state.app, &req.connection_id, &req.name)
.await
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -338,7 +340,7 @@ pub async fn create_tenant(
ensure_writable(&state.app, &req.connection_id, "Create tenant").await?;
dbx_core::mq::service::mq_create_tenant_core(&state.app, &req.connection_id, &req.name, req.config)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -349,7 +351,7 @@ pub async fn update_tenant(
ensure_writable(&state.app, &req.connection_id, "Update tenant").await?;
dbx_core::mq::service::mq_update_tenant_core(&state.app, &req.connection_id, &req.name, req.config)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -360,7 +362,7 @@ pub async fn delete_tenant(
ensure_writable(&state.app, &req.connection_id, "Delete tenant").await?;
dbx_core::mq::service::mq_delete_tenant_core(&state.app, &req.connection_id, &req.name, req.force)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -370,7 +372,7 @@ pub async fn list_namespaces(
) -> Result<Json<Vec<dbx_core::mq::NamespaceInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_namespaces_core(&state.app, &req.connection_id, &req.tenant)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -381,7 +383,7 @@ pub async fn create_namespace(
ensure_writable(&state.app, &req.connection_id, "Create namespace").await?;
dbx_core::mq::service::mq_create_namespace_core(&state.app, &req.connection_id, req.ns, req.config)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -392,7 +394,7 @@ pub async fn delete_namespace(
ensure_writable(&state.app, &req.connection_id, "Delete namespace").await?;
dbx_core::mq::service::mq_delete_namespace_core(&state.app, &req.connection_id, req.ns, req.force)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -402,7 +404,7 @@ pub async fn get_namespace_policies(
) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_get_namespace_policies_core(&state.app, &req.connection_id, req.ns)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -412,7 +414,7 @@ pub async fn list_topics(
) -> Result<Json<Vec<dbx_core::mq::TopicInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_topics_core(&state.app, &req.connection_id, req.ns, req.opts)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -423,7 +425,7 @@ pub async fn create_topic(
ensure_writable(&state.app, &req.connection_id, "Create topic").await?;
dbx_core::mq::service::mq_create_topic_core(&state.app, &req.connection_id, req.topic, req.partitions)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -434,7 +436,7 @@ pub async fn delete_topic(
ensure_writable(&state.app, &req.connection_id, "Delete topic").await?;
dbx_core::mq::service::mq_delete_topic_core(&state.app, &req.connection_id, req.topic, req.force)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -445,7 +447,7 @@ pub async fn update_partitions(
ensure_writable(&state.app, &req.connection_id, "Update partitions").await?;
dbx_core::mq::service::mq_update_partitions_core(&state.app, &req.connection_id, req.topic, req.partitions)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -455,7 +457,7 @@ pub async fn get_topic_stats(
) -> Result<Json<dbx_core::mq::TopicStats>, AppError> {
let result = dbx_core::mq::service::mq_get_topic_stats_core(&state.app, &req.connection_id, req.topic)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -465,7 +467,7 @@ pub async fn get_topic_internal_stats(
) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_get_topic_internal_stats_core(&state.app, &req.connection_id, req.topic)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -475,7 +477,7 @@ pub async fn list_subscriptions(
) -> Result<Json<Vec<dbx_core::mq::SubscriptionInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_subscriptions_core(&state.app, &req.connection_id, req.topic)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -486,7 +488,7 @@ pub async fn create_subscription(
ensure_writable(&state.app, &req.connection_id, "Create subscription").await?;
dbx_core::mq::service::mq_create_subscription_core(&state.app, &req.connection_id, req.topic, req.sub, req.pos)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -497,7 +499,7 @@ pub async fn delete_subscription(
ensure_writable(&state.app, &req.connection_id, "Delete subscription").await?;
dbx_core::mq::service::mq_delete_subscription_core(&state.app, &req.connection_id, req.topic, req.sub, req.force)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -508,7 +510,7 @@ pub async fn skip_messages(
ensure_writable(&state.app, &req.connection_id, "Skip messages").await?;
dbx_core::mq::service::mq_skip_messages_core(&state.app, &req.connection_id, req.topic, req.sub, req.count)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -519,7 +521,7 @@ pub async fn reset_cursor(
ensure_writable(&state.app, &req.connection_id, "Reset cursor").await?;
dbx_core::mq::service::mq_reset_cursor_core(&state.app, &req.connection_id, req.topic, req.sub, req.pos)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -530,7 +532,7 @@ pub async fn clear_backlog(
ensure_writable(&state.app, &req.connection_id, "Clear backlog").await?;
dbx_core::mq::service::mq_clear_backlog_core(&state.app, &req.connection_id, req.topic, req.sub)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -540,7 +542,7 @@ pub async fn get_consumer_group_config(
) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_get_consumer_group_config_core(&state.app, &req.connection_id, req.group_id)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -556,7 +558,7 @@ pub async fn alter_consumer_group_config(
req.config,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -573,7 +575,7 @@ pub async fn peek_messages(
req.options,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -590,7 +592,7 @@ pub async fn expire_messages(
req.expire_seconds,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -600,7 +602,7 @@ pub async fn list_producers(
) -> Result<Json<Vec<dbx_core::mq::ProducerInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_producers_core(&state.app, &req.connection_id, req.topic)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -610,13 +612,15 @@ pub async fn list_consumers(
) -> Result<Json<Vec<dbx_core::mq::ConsumerInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_consumers_core(&state.app, &req.connection_id, req.topic, req.sub)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
pub async fn unload_topic(State(state): State<Arc<WebState>>, Json(req): Json<TopicReq>) -> Result<Json<()>, AppError> {
ensure_writable(&state.app, &req.connection_id, "Unload topic").await?;
dbx_core::mq::service::mq_unload_topic_core(&state.app, &req.connection_id, req.topic).await.map_err(AppError)?;
dbx_core::mq::service::mq_unload_topic_core(&state.app, &req.connection_id, req.topic)
.await
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -627,7 +631,7 @@ pub async fn set_publish_rate(
ensure_writable(&state.app, &req.connection_id, "Set publish rate").await?;
dbx_core::mq::service::mq_set_publish_rate_core(&state.app, &req.connection_id, req.scope, req.rate)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -638,7 +642,7 @@ pub async fn set_dispatch_rate(
ensure_writable(&state.app, &req.connection_id, "Set dispatch rate").await?;
dbx_core::mq::service::mq_set_dispatch_rate_core(&state.app, &req.connection_id, req.scope, req.rate)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -649,7 +653,7 @@ pub async fn set_subscribe_rate(
ensure_writable(&state.app, &req.connection_id, "Set subscribe rate").await?;
dbx_core::mq::service::mq_set_subscribe_rate_core(&state.app, &req.connection_id, req.scope, req.rate)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -660,7 +664,7 @@ pub async fn set_backlog_quota(
ensure_writable(&state.app, &req.connection_id, "Set backlog quota").await?;
dbx_core::mq::service::mq_set_backlog_quota_core(&state.app, &req.connection_id, req.scope, req.quota)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -671,7 +675,7 @@ pub async fn set_retention(
ensure_writable(&state.app, &req.connection_id, "Set retention").await?;
dbx_core::mq::service::mq_set_retention_core(&state.app, &req.connection_id, req.scope, req.retention)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -681,7 +685,7 @@ pub async fn get_effective_policies(
) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_get_effective_policies_core(&state.app, &req.connection_id, req.scope)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -692,7 +696,7 @@ pub async fn grant_permission(
ensure_writable(&state.app, &req.connection_id, "Grant permission").await?;
dbx_core::mq::service::mq_grant_permission_core(&state.app, &req.connection_id, req.scope, req.role, req.actions)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -703,7 +707,7 @@ pub async fn revoke_permission(
ensure_writable(&state.app, &req.connection_id, "Revoke permission").await?;
dbx_core::mq::service::mq_revoke_permission_core(&state.app, &req.connection_id, req.scope, req.role)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -713,7 +717,7 @@ pub async fn list_permissions(
) -> Result<Json<dbx_core::mq::PermissionMap>, AppError> {
let result = dbx_core::mq::service::mq_list_permissions_core(&state.app, &req.connection_id, req.scope)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -722,8 +726,9 @@ pub async fn issue_token(
Json(req): Json<IssueTokenReq>,
) -> Result<Json<dbx_core::mq::MqIssuedToken>, AppError> {
ensure_writable(&state.app, &req.connection_id, "Issue MQ token").await?;
let result =
dbx_core::mq::service::mq_issue_token_core(&state.app, &req.connection_id, req.req).await.map_err(AppError)?;
let result = dbx_core::mq::service::mq_issue_token_core(&state.app, &req.connection_id, req.req)
.await
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -733,7 +738,7 @@ pub async fn list_token_records(
) -> Result<Json<Vec<dbx_core::mq::MqTokenRecord>>, AppError> {
let result = dbx_core::mq::service::mq_list_token_records_core(&state.app, &req.connection_id, req.subject)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -743,7 +748,7 @@ pub async fn get_backlog(
) -> Result<Json<dbx_core::mq::BacklogStats>, AppError> {
let result = dbx_core::mq::service::mq_get_backlog_core(&state.app, &req.connection_id, req.topic, req.sub)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -757,8 +762,9 @@ pub async fn get_cluster_info(
State(state): State<Arc<WebState>>,
Json(req): Json<ClusterInfoReq>,
) -> Result<Json<dbx_core::mq::ClusterInfo>, AppError> {
let result =
dbx_core::mq::service::mq_get_cluster_info_core(&state.app, &req.connection_id).await.map_err(AppError)?;
let result = dbx_core::mq::service::mq_get_cluster_info_core(&state.app, &req.connection_id)
.await
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -768,7 +774,7 @@ pub async fn get_topic_route(
) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_get_topic_route_core(&state.app, &req.connection_id, req.topic)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -787,7 +793,7 @@ pub async fn alter_topic_config(
ensure_writable(&state.app, &req.connection_id, "Alter topic config").await?;
dbx_core::mq::service::mq_alter_topic_config_core(&state.app, &req.connection_id, req.topic, req.configs)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -798,7 +804,7 @@ pub async fn skip_topic_accumulation(
ensure_writable(&state.app, &req.connection_id, "Skip topic accumulation").await?;
let result = dbx_core::mq::service::mq_skip_topic_accumulation_core(&state.app, &req.connection_id, req.topic)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -816,7 +822,7 @@ pub async fn view_message(
) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_view_message_core(&state.app, &req.connection_id, req.topic, req.msg_id)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -845,7 +851,7 @@ pub async fn query_messages_by_key(
req.max_num,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -872,7 +878,7 @@ pub async fn query_messages_by_topic(
req.max_num,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -891,7 +897,7 @@ pub async fn query_message_trace(
let result =
dbx_core::mq::service::mq_query_message_trace_core(&state.app, &req.connection_id, req.msg_id, req.trace_topic)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -902,8 +908,9 @@ pub async fn raw_request(
if req.req.is_mutating() {
ensure_writable(&state.app, &req.connection_id, "MQ admin write").await?;
}
let result =
dbx_core::mq::service::mq_raw_request_core(&state.app, &req.connection_id, req.req).await.map_err(AppError)?;
let result = dbx_core::mq::service::mq_raw_request_core(&state.app, &req.connection_id, req.req)
.await
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -920,7 +927,8 @@ pub async fn send_message(
State(state): State<Arc<WebState>>,
Json(req): Json<SendMessageReq>,
) -> Result<Json<dbx_core::mq::SendMessageResponse>, AppError> {
let result =
dbx_core::mq::service::mq_send_message_core(&state.app, &req.connection_id, req.req).await.map_err(AppError)?;
let result = dbx_core::mq::service::mq_send_message_core(&state.app, &req.connection_id, req.req)
.await
.map_err(AppError::from)?;
Ok(Json(result))
}
+19 -17
View File
@@ -100,8 +100,9 @@ pub async fn test_connection(
State(state): State<Arc<WebState>>,
Json(req): Json<ConnReq>,
) -> Result<Json<dbx_core::nacos::NacosConnectionInfo>, AppError> {
let result =
dbx_core::nacos::service::nacos_test_connection_core(&state.app, &req.connection_id).await.map_err(AppError)?;
let result = dbx_core::nacos::service::nacos_test_connection_core(&state.app, &req.connection_id)
.await
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -109,8 +110,9 @@ pub async fn list_namespaces(
State(state): State<Arc<WebState>>,
Json(req): Json<ConnReq>,
) -> Result<Json<Vec<dbx_core::nacos::NacosNamespaceInfo>>, AppError> {
let result =
dbx_core::nacos::service::nacos_list_namespaces_core(&state.app, &req.connection_id).await.map_err(AppError)?;
let result = dbx_core::nacos::service::nacos_list_namespaces_core(&state.app, &req.connection_id)
.await
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -120,7 +122,7 @@ pub async fn create_namespace(
) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_create_namespace_core(&state.app, &req.connection_id, req.req)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -130,7 +132,7 @@ pub async fn update_namespace(
) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_update_namespace_core(&state.app, &req.connection_id, req.req)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -140,7 +142,7 @@ pub async fn list_configs(
) -> Result<Json<dbx_core::nacos::NacosConfigList>, AppError> {
let result = dbx_core::nacos::service::nacos_list_configs_core(&state.app, &req.connection_id, req.query)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -150,7 +152,7 @@ pub async fn get_config(
) -> Result<Json<dbx_core::nacos::NacosConfigItem>, AppError> {
let result = dbx_core::nacos::service::nacos_get_config_core(&state.app, &req.connection_id, req.key)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -160,7 +162,7 @@ pub async fn publish_config(
) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_publish_config_core(&state.app, &req.connection_id, req.req)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -170,7 +172,7 @@ pub async fn delete_config(
) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_delete_config_core(&state.app, &req.connection_id, req.key)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -180,7 +182,7 @@ pub async fn list_config_history(
) -> Result<Json<dbx_core::nacos::NacosConfigHistoryList>, AppError> {
let result = dbx_core::nacos::service::nacos_list_config_history_core(&state.app, &req.connection_id, req.query)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -190,7 +192,7 @@ pub async fn get_config_history(
) -> Result<Json<dbx_core::nacos::NacosConfigItem>, AppError> {
let result = dbx_core::nacos::service::nacos_get_config_history_core(&state.app, &req.connection_id, req.key)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -200,7 +202,7 @@ pub async fn rollback_config(
) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_rollback_config_core(&state.app, &req.connection_id, req.req)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -210,7 +212,7 @@ pub async fn list_services(
) -> Result<Json<dbx_core::nacos::NacosServiceList>, AppError> {
let result = dbx_core::nacos::service::nacos_list_services_core(&state.app, &req.connection_id, req.query)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -220,7 +222,7 @@ pub async fn list_instances(
) -> Result<Json<Vec<dbx_core::nacos::NacosInstanceInfo>>, AppError> {
let result = dbx_core::nacos::service::nacos_list_instances_core(&state.app, &req.connection_id, req.query)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -230,7 +232,7 @@ pub async fn update_instance(
) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_update_instance_core(&state.app, &req.connection_id, req.req)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -240,6 +242,6 @@ pub async fn raw_request(
) -> Result<Json<dbx_core::nacos::NacosRawResponse>, AppError> {
let result = dbx_core::nacos::service::nacos_raw_request_core(&state.app, &req.connection_id, req.req)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
+1 -1
View File
@@ -8,5 +8,5 @@ use crate::error::AppError;
use crate::state::WebState;
pub async fn list_plugins(State(state): State<Arc<WebState>>) -> Result<Json<Vec<InstalledPlugin>>, AppError> {
state.app.plugins.list_installed().map(Json).map_err(AppError)
state.app.plugins.list_installed().map(Json).map_err(AppError::from)
}
@@ -0,0 +1,97 @@
use std::sync::Arc;
use axum::extract::{Path, State};
use axum::Json;
use serde::{Deserialize, Serialize};
use dbx_core::prompt_template::PromptTemplate;
use crate::error::AppError;
use crate::state::WebState;
// ---------------------------------------------------------------------------
// Request types
// ---------------------------------------------------------------------------
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SavePromptTemplateRequest {
pub id: String,
pub name: String,
pub content: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SetGlobalInstructionsRequest {
pub content: String,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetGlobalInstructionsResponse {
pub content: String,
}
// ---------------------------------------------------------------------------
// Prompt Templates CRUD
// ---------------------------------------------------------------------------
pub async fn load_prompt_templates(State(state): State<Arc<WebState>>) -> Result<Json<Vec<PromptTemplate>>, AppError> {
let templates = state.app.storage.load_prompt_templates().await.map_err(AppError::from)?;
Ok(Json(templates))
}
pub async fn save_prompt_template(
State(state): State<Arc<WebState>>,
Json(body): Json<SavePromptTemplateRequest>,
) -> Result<Json<PromptTemplate>, AppError> {
let result = state.app.storage.save_prompt_template(&body.id, &body.name, &body.content).await.map_err(|e| {
// Map validation errors to 400
if e.contains("too long") || e.contains("cannot be empty") || e.contains("duplicate") {
AppError::bad_request(e)
} else {
AppError::from(e)
}
})?;
Ok(Json(result))
}
pub async fn delete_prompt_template(
State(state): State<Arc<WebState>>,
Path(id): Path<String>,
) -> Result<Json<()>, AppError> {
state.app.storage.delete_prompt_template(&id).await.map_err(|e| {
if e.contains("not found") {
AppError::not_found(e)
} else {
AppError::from(e)
}
})?;
Ok(Json(()))
}
// ---------------------------------------------------------------------------
// Global Custom Instructions
// ---------------------------------------------------------------------------
pub async fn get_global_instructions(
State(state): State<Arc<WebState>>,
) -> Result<Json<GetGlobalInstructionsResponse>, AppError> {
let content = state.app.storage.load_ai_global_custom_instructions().await.map_err(AppError::from)?;
Ok(Json(GetGlobalInstructionsResponse { content }))
}
pub async fn set_global_instructions(
State(state): State<Arc<WebState>>,
Json(body): Json<SetGlobalInstructionsRequest>,
) -> Result<Json<()>, AppError> {
state.app.storage.save_ai_global_custom_instructions(&body.content).await.map_err(|e| {
if e.contains("too long") {
AppError::bad_request(e)
} else {
AppError::from(e)
}
})?;
Ok(Json(()))
}
+24 -22
View File
@@ -353,7 +353,7 @@ pub async fn execute_query(
},
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
drop(registered);
Ok(Json(result))
@@ -396,7 +396,7 @@ pub async fn execute_multi(
},
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
drop(registered);
Ok(Json(result))
@@ -420,7 +420,7 @@ pub async fn execute_batch(
req.timeout_secs,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -445,7 +445,7 @@ pub async fn close_query_session(
req.client_session_id.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!(closed)))
}
@@ -459,7 +459,7 @@ pub async fn close_client_connection_session(
.app
.close_client_session_pool(&req.connection_id, database, &req.client_session_id)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!(closed)))
}
@@ -485,7 +485,7 @@ pub async fn execute_script(
None,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -503,7 +503,7 @@ pub async fn execute_in_transaction(
req.schema.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -511,7 +511,7 @@ pub async fn execute_in_transaction(
pub async fn analyze_sql_references(
Json(req): Json<AnalyzeSqlReferencesRequest>,
) -> Result<Json<dbx_core::sql_analysis::SqlReferenceAnalysis>, AppError> {
dbx_core::sql_analysis::analyze_sql_references(&req.sql, req.dialect.as_deref()).map(Json).map_err(AppError)
dbx_core::sql_analysis::analyze_sql_references(&req.sql, req.dialect.as_deref()).map(Json).map_err(AppError::from)
}
pub async fn find_statement_at_cursor(Json(req): Json<FindStatementAtCursorRequest>) -> Json<String> {
@@ -571,7 +571,7 @@ pub async fn get_explain_info(
req.mode.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(plan))
}
@@ -600,13 +600,13 @@ pub async fn build_search_result_where(Json(req): Json<BuildSearchResultWhereReq
}
pub async fn build_rename_object_sql(Json(req): Json<BuildRenameObjectSqlRequest>) -> Result<Json<String>, AppError> {
dbx_core::db_admin_sql::build_rename_object_sql(req.options).map(Json).map_err(AppError)
dbx_core::db_admin_sql::build_rename_object_sql(req.options).map(Json).map_err(AppError::from)
}
pub async fn build_create_database_sql(
Json(req): Json<BuildCreateDatabaseSqlRequest>,
) -> Result<Json<String>, AppError> {
dbx_core::db_admin_sql::build_create_database_sql(req.options).map(Json).map_err(AppError)
dbx_core::db_admin_sql::build_create_database_sql(req.options).map(Json).map_err(AppError::from)
}
#[cfg(feature = "duckdb-bundled")]
@@ -629,7 +629,7 @@ pub async fn build_drop_table_sql(Json(req): Json<BuildTableAdminSqlRequest>) ->
pub async fn build_drop_table_child_object_sql(
Json(req): Json<BuildDropTableChildObjectSqlRequest>,
) -> Result<Json<String>, AppError> {
dbx_core::db_admin_sql::build_drop_table_child_object_sql(req.options).map(Json).map_err(AppError)
dbx_core::db_admin_sql::build_drop_table_child_object_sql(req.options).map(Json).map_err(AppError::from)
}
pub async fn build_empty_table_sql(Json(req): Json<BuildTableAdminSqlRequest>) -> Json<String> {
@@ -645,13 +645,13 @@ pub async fn build_drop_database_sql(Json(req): Json<BuildDatabaseNameSqlRequest
}
pub async fn build_create_schema_sql(Json(req): Json<BuildSchemaNameSqlRequest>) -> Result<Json<String>, AppError> {
dbx_core::db_admin_sql::build_create_schema_sql(req.options).map(Json).map_err(AppError)
dbx_core::db_admin_sql::build_create_schema_sql(req.options).map(Json).map_err(AppError::from)
}
pub async fn build_update_database_properties_sql(
Json(req): Json<BuildDatabasePropertyEditSqlRequest>,
) -> Result<Json<String>, AppError> {
dbx_core::db_admin_sql::build_update_database_properties_sql(req.options).map(Json).map_err(AppError)
dbx_core::db_admin_sql::build_update_database_properties_sql(req.options).map(Json).map_err(AppError::from)
}
pub async fn build_drop_schema_sql(Json(req): Json<BuildSchemaNameSqlRequest>) -> Json<String> {
@@ -671,13 +671,13 @@ pub async fn build_copy_table_data_sql(Json(req): Json<BuildCopyTableDataSqlRequ
pub async fn build_executable_object_source_statements(
Json(req): Json<BuildExecutableObjectSourceRequest>,
) -> Result<Json<Vec<String>>, AppError> {
dbx_core::object_source_sql::build_executable_object_source_statements(req.input).map(Json).map_err(AppError)
dbx_core::object_source_sql::build_executable_object_source_statements(req.input).map(Json).map_err(AppError::from)
}
pub async fn build_executable_object_source_sql(
Json(req): Json<BuildExecutableObjectSourceRequest>,
) -> Result<Json<String>, AppError> {
dbx_core::object_source_sql::build_executable_object_source_sql(req.input).map(Json).map_err(AppError)
dbx_core::object_source_sql::build_executable_object_source_sql(req.input).map(Json).map_err(AppError::from)
}
pub async fn build_editable_object_source(Json(req): Json<BuildExecutableObjectSourceRequest>) -> Json<String> {
@@ -687,7 +687,9 @@ pub async fn build_editable_object_source(Json(req): Json<BuildExecutableObjectS
pub async fn build_routine_rename_object_source_statements(
Json(req): Json<BuildRoutineRenameObjectSourceRequest>,
) -> Result<Json<Vec<String>>, AppError> {
dbx_core::object_source_sql::build_routine_rename_object_source_statements(req.input).map(Json).map_err(AppError)
dbx_core::object_source_sql::build_routine_rename_object_source_statements(req.input)
.map(Json)
.map_err(AppError::from)
}
pub async fn build_view_ddl_sql(Json(req): Json<BuildViewDdlRequest>) -> Json<String> {
@@ -712,7 +714,7 @@ pub async fn preview_sqlite_table_structure_change(
)
.await
.map(Json)
.map_err(AppError)
.map_err(AppError::from)
}
pub async fn apply_sqlite_table_structure_change(
@@ -728,7 +730,7 @@ pub async fn apply_sqlite_table_structure_change(
)
.await
.map(Json)
.map_err(AppError)
.map_err(AppError::from)
}
pub async fn build_create_table_sql(
@@ -802,11 +804,11 @@ pub async fn build_hive_table_properties_sql(Json(req): Json<BuildHiveTablePrope
pub async fn build_export_insert_statements(
Json(req): Json<BuildExportInsertStatementsRequest>,
) -> Result<Json<Vec<String>>, AppError> {
dbx_core::database_export::build_export_insert_statements(req.options).map(Json).map_err(AppError)
dbx_core::database_export::build_export_insert_statements(req.options).map(Json).map_err(AppError::from)
}
pub async fn build_export_sql_insert(Json(req): Json<BuildExportSqlInsertRequest>) -> Result<Json<String>, AppError> {
dbx_core::database_export::build_export_sql_insert(req.options).map(Json).map_err(AppError)
dbx_core::database_export::build_export_sql_insert(req.options).map(Json).map_err(AppError::from)
}
pub async fn build_database_sql_export(
@@ -841,5 +843,5 @@ pub async fn build_database_sql_export(
}
}
}
dbx_core::database_export::build_database_sql_export(options).map(Json).map_err(AppError)
dbx_core::database_export::build_database_sql_export(options).map(Json).map_err(AppError::from)
}
@@ -43,10 +43,10 @@ pub async fn start_query_result_export(
let ext = match req.format.as_str() {
"csv" => "csv",
"xlsx" => "xlsx",
_ => return Err(AppError(format!("Unsupported query result export format: {}", req.format))),
_ => return Err(AppError::from(format!("Unsupported query result export format: {}", req.format))),
};
let tmp_dir = state.data_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?;
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
let tmp_file = tmp_dir.join(format!("query_result_export_{export_id}.{ext}"));
let file_path = tmp_file.to_string_lossy().to_string();
req.file_path = file_path.clone();
@@ -146,15 +146,15 @@ pub async fn query_result_export_download(
.write()
.await
.remove(&export_id)
.ok_or_else(|| AppError("Export file not found".to_string()))?;
.ok_or_else(|| AppError::from("Export file not found".to_string()))?;
let data = tokio::fs::read(&file_path).await.map_err(|e| AppError(e.to_string()))?;
let data = tokio::fs::read(&file_path).await.map_err(|e| AppError::from(e.to_string()))?;
let _ = tokio::fs::remove_file(&file_path).await;
let (content_type, file_ext) = match format.as_str() {
"csv" => ("text/csv; charset=utf-8", "csv"),
"xlsx" => ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"),
_ => return Err(AppError(format!("Unknown format: {format}"))),
_ => return Err(AppError::from(format!("Unknown format: {format}"))),
};
let filename = format!("query_result_export_{export_id}.{file_ext}");
@@ -164,5 +164,5 @@ pub async fn query_result_export_download(
.header(header::CONTENT_TYPE, content_type)
.header(header::CONTENT_DISPOSITION, format!("attachment; filename=\"{filename}\""))
.body(Body::from(data))
.map_err(|e| AppError(e.to_string()))
.map_err(|e| AppError::from(e.to_string()))
}
+39 -38
View File
@@ -15,7 +15,7 @@ async fn ensure_writable(
action: &str,
) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
return Err(AppError(format!(
return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
name, action
)));
@@ -215,8 +215,8 @@ pub async fn list_databases(
Json(req): Json<RedisConnectionRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
let result =
dbx_core::redis_ops::redis_list_databases_core(&state.app, &req.connection_id).await.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
dbx_core::redis_ops::redis_list_databases_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn scan_keys(
@@ -232,8 +232,8 @@ pub async fn scan_keys(
req.count,
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn scan_keys_batch(
@@ -251,8 +251,8 @@ pub async fn scan_keys_batch(
req.include_types.unwrap_or(true),
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn scan_values(
@@ -270,8 +270,8 @@ pub async fn scan_values(
req.count,
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn get_value(
@@ -280,8 +280,8 @@ pub async fn get_value(
) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::redis_ops::redis_get_value_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn load_more(
@@ -299,8 +299,8 @@ pub async fn load_more(
req.filter.as_deref(),
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn set_string(
@@ -317,7 +317,7 @@ pub async fn set_string(
req.ttl,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -328,7 +328,7 @@ pub async fn delete_key(
ensure_writable(&state.app, &req.connection_id, "Delete key").await?;
dbx_core::redis_ops::redis_delete_key_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -348,7 +348,7 @@ pub async fn hash_set(
req.ttl,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -359,7 +359,7 @@ pub async fn hash_del(
ensure_writable(&state.app, &req.connection_id, "HDEL").await?;
dbx_core::redis_ops::redis_hash_del_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, &req.field)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -378,7 +378,7 @@ pub async fn list_push(
req.ttl,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -391,7 +391,7 @@ pub async fn list_set(
let value = req.value.as_deref().unwrap_or("");
dbx_core::redis_ops::redis_list_set_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, index, value)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -403,7 +403,7 @@ pub async fn list_remove(
let index = req.index.unwrap_or(0);
dbx_core::redis_ops::redis_list_remove_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, index)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -421,7 +421,7 @@ pub async fn set_add(
req.ttl,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -432,7 +432,7 @@ pub async fn set_remove(
ensure_writable(&state.app, &req.connection_id, "SREM").await?;
dbx_core::redis_ops::redis_set_remove_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, &req.member)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -447,7 +447,7 @@ pub async fn zadd(State(state): State<Arc<WebState>>, Json(req): Json<RedisZaddR
req.ttl,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -465,7 +465,7 @@ pub async fn stream_add(
req.ttl,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -482,7 +482,7 @@ pub async fn json_set(
req.ttl,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -492,7 +492,7 @@ pub async fn check_json_module(
) -> Result<Json<bool>, AppError> {
let result = dbx_core::redis_ops::redis_check_json_module_in_db_core(&state.app, &req.connection_id, req.db)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -504,7 +504,7 @@ pub async fn delete_keys(
let result =
dbx_core::redis_ops::redis_delete_keys_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raws)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -513,7 +513,7 @@ pub async fn flush_db(
Json(req): Json<RedisDbRequest>,
) -> Result<Json<()>, AppError> {
ensure_writable(&state.app, &req.connection_id, "FLUSHDB").await?;
dbx_core::redis_ops::redis_flush_db_core(&state.app, &req.connection_id, req.db).await.map_err(AppError)?;
dbx_core::redis_ops::redis_flush_db_core(&state.app, &req.connection_id, req.db).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -524,7 +524,7 @@ pub async fn execute_command(
) -> Result<Json<serde_json::Value>, AppError> {
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
let argv = dbx_core::db::redis_driver::parse_command_argv(&req.command)
.map_err(|error| AppError(format!("Invalid Redis command: {error}")))?;
.map_err(|error| AppError::from(format!("Invalid Redis command: {error}")))?;
let cmd_name = argv[0].to_ascii_uppercase();
let safety = dbx_core::db::redis_driver::classify_command(&cmd_name);
let is_mcp_request = super::mcp_policy::is_mcp_request(&headers);
@@ -554,7 +554,7 @@ pub async fn execute_command(
// In read-only mode, only allow safe read commands
if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.connection_id).await {
if safety != dbx_core::db::redis_driver::RedisCommandSafety::Allowed {
return Err(AppError(format!(
return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. Command '{}' blocked.",
name, cmd_name
)));
@@ -568,8 +568,8 @@ pub async fn execute_command(
if is_mcp_request { centrally_approved_high_risk } else { req.skip_safety_check.unwrap_or(false) },
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn publish_message(
@@ -580,7 +580,7 @@ pub async fn publish_message(
let count =
dbx_core::redis_ops::redis_publish_core(&state.app, &req.connection_id, req.db, &req.channel, &req.message)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "subscribers": count })))
}
@@ -596,15 +596,16 @@ pub async fn slowlog_get(
req.node_port,
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn cluster_master_nodes(
State(state): State<Arc<WebState>>,
Json(req): Json<ClusterNodesRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
let result =
dbx_core::redis_ops::redis_cluster_master_nodes_core(&state.app, &req.connection_id).await.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
let result = dbx_core::redis_ops::redis_cluster_master_nodes_core(&state.app, &req.connection_id)
.await
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
+6 -6
View File
@@ -8,7 +8,7 @@ use crate::error::AppError;
use crate::state::WebState;
pub async fn load_saved_sql_library(State(state): State<Arc<WebState>>) -> Result<Json<SavedSqlLibrary>, AppError> {
let library = state.app.storage.load_saved_sql_library_summary().await.map_err(AppError)?;
let library = state.app.storage.load_saved_sql_library_summary().await.map_err(AppError::from)?;
Ok(Json(library))
}
@@ -16,7 +16,7 @@ pub async fn load_saved_sql_file(
State(state): State<Arc<WebState>>,
Path(id): Path<String>,
) -> Result<Json<Option<SavedSqlFile>>, AppError> {
let file = state.app.storage.load_saved_sql_file(&id).await.map_err(AppError)?;
let file = state.app.storage.load_saved_sql_file(&id).await.map_err(AppError::from)?;
Ok(Json(file))
}
@@ -24,7 +24,7 @@ pub async fn save_saved_sql_folder(
State(state): State<Arc<WebState>>,
Json(folder): Json<SavedSqlFolder>,
) -> Result<Json<SavedSqlFolder>, AppError> {
state.app.storage.save_saved_sql_folder(&folder).await.map_err(AppError)?;
state.app.storage.save_saved_sql_folder(&folder).await.map_err(AppError::from)?;
Ok(Json(folder))
}
@@ -32,7 +32,7 @@ pub async fn delete_saved_sql_folder(
State(state): State<Arc<WebState>>,
Path(id): Path<String>,
) -> Result<Json<()>, AppError> {
state.app.storage.delete_saved_sql_folder(&id).await.map_err(AppError)?;
state.app.storage.delete_saved_sql_folder(&id).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -40,7 +40,7 @@ pub async fn save_saved_sql_file(
State(state): State<Arc<WebState>>,
Json(file): Json<SavedSqlFile>,
) -> Result<Json<SavedSqlFile>, AppError> {
state.app.storage.save_saved_sql_file(&file).await.map_err(AppError)?;
state.app.storage.save_saved_sql_file(&file).await.map_err(AppError::from)?;
Ok(Json(file))
}
@@ -48,6 +48,6 @@ pub async fn delete_saved_sql_file(
State(state): State<Arc<WebState>>,
Path(id): Path<String>,
) -> Result<Json<()>, AppError> {
state.app.storage.delete_saved_sql_file(&id).await.map_err(AppError)?;
state.app.storage.delete_saved_sql_file(&id).await.map_err(AppError::from)?;
Ok(Json(()))
}
+65 -60
View File
@@ -29,8 +29,8 @@ pub async fn list_databases(
State(state): State<Arc<WebState>>,
Query(q): Query<SchemaQuery>,
) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::schema::list_databases_core(&state.app, &q.connection_id).await.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
let result = dbx_core::schema::list_databases_core(&state.app, &q.connection_id).await.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
/// Resolve a non-internal catalog for dispatch to the Doris multi-catalog path.
@@ -42,8 +42,9 @@ pub async fn list_doris_catalogs(
State(state): State<Arc<WebState>>,
Query(q): Query<SchemaQuery>,
) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::schema::list_doris_catalogs_core(&state.app, &q.connection_id).await.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
let result =
dbx_core::schema::list_doris_catalogs_core(&state.app, &q.connection_id).await.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_doris_catalog_databases(
@@ -53,17 +54,18 @@ pub async fn list_doris_catalog_databases(
let catalog = q.catalog.as_deref().unwrap_or("internal");
let result = dbx_core::schema::list_doris_catalog_databases_core(&state.app, &q.connection_id, catalog)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_sqlserver_linked_servers(
State(state): State<Arc<WebState>>,
Query(q): Query<SchemaQuery>,
) -> Result<Json<serde_json::Value>, AppError> {
let result =
dbx_core::schema::list_sqlserver_linked_servers_core(&state.app, &q.connection_id).await.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
let result = dbx_core::schema::list_sqlserver_linked_servers_core(&state.app, &q.connection_id)
.await
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_sqlserver_linked_server_catalogs(
@@ -73,8 +75,8 @@ pub async fn list_sqlserver_linked_server_catalogs(
let server = q.server.as_deref().unwrap_or("");
let result = dbx_core::schema::list_sqlserver_linked_server_catalogs_core(&state.app, &q.connection_id, server)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_sqlserver_linked_server_schemas(
@@ -86,7 +88,7 @@ pub async fn list_sqlserver_linked_server_schemas(
let result =
dbx_core::schema::list_sqlserver_linked_server_schemas_core(&state.app, &q.connection_id, server, catalog)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -108,8 +110,8 @@ pub async fn list_sqlserver_linked_server_tables(
q.offset,
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn get_sqlserver_column_metadata(
@@ -122,8 +124,8 @@ pub async fn get_sqlserver_column_metadata(
let result =
dbx_core::schema::get_sqlserver_column_metadata_core(&state.app, &q.connection_id, database, schema, table)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_schemas(
@@ -138,7 +140,7 @@ pub async fn list_schemas(
q.apply_visible_filter.unwrap_or(false),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -163,7 +165,7 @@ pub async fn list_tables(
object_types.as_deref(),
)
.await
.map_err(AppError)?
.map_err(AppError::from)?
} else {
dbx_core::schema::list_tables_core(
&state.app,
@@ -176,9 +178,9 @@ pub async fn list_tables(
object_types.as_deref(),
)
.await
.map_err(AppError)?
.map_err(AppError::from)?
};
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_objects(
@@ -202,7 +204,7 @@ pub async fn list_objects(
object_types.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
tables
.into_iter()
.map(|table| dbx_core::db::ObjectInfo {
@@ -230,9 +232,9 @@ pub async fn list_objects(
object_types.as_deref(),
)
.await
.map_err(AppError)?
.map_err(AppError::from)?
};
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_object_statistics(
@@ -243,8 +245,8 @@ pub async fn list_object_statistics(
let schema = q.schema.as_deref().unwrap_or("");
let result = dbx_core::schema::list_object_statistics_core(&state.app, &q.connection_id, database, schema)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_completion_objects(
@@ -255,15 +257,16 @@ pub async fn list_completion_objects(
let schema = q.schema.as_deref().unwrap_or("");
let result = dbx_core::schema::list_completion_objects_core(&state.app, &q.connection_id, database, schema)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn completion_assistant_search(
State(state): State<Arc<WebState>>,
Json(request): Json<dbx_core::db::CompletionAssistantRequest>,
) -> Result<Json<dbx_core::db::CompletionAssistantResponse>, AppError> {
let result = dbx_core::schema::completion_assistant_search_core(&state.app, request).await.map_err(AppError)?;
let result =
dbx_core::schema::completion_assistant_search_core(&state.app, request).await.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -274,7 +277,7 @@ pub async fn get_object_source(
let database = q.database.as_deref().unwrap_or("");
let schema = q.schema.as_deref().unwrap_or("");
let name = q.table.as_deref().unwrap_or("");
let object_type = q.object_type.ok_or_else(|| AppError("Missing object_type".to_string()))?;
let object_type = q.object_type.ok_or_else(|| AppError::from("Missing object_type".to_string()))?;
let result = dbx_core::schema::get_object_source_core(
&state.app,
&q.connection_id,
@@ -285,7 +288,7 @@ pub async fn get_object_source(
q.signature.as_deref(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -299,7 +302,7 @@ pub async fn list_columns(
let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await {
dbx_core::schema::get_doris_catalog_columns_core(&state.app, &q.connection_id, &catalog, database, table)
.await
.map_err(AppError)?
.map_err(AppError::from)?
} else {
dbx_core::schema::get_columns_core_for_session(
&state.app,
@@ -310,9 +313,9 @@ pub async fn list_columns(
q.client_session_id.as_deref(),
)
.await
.map_err(AppError)?
.map_err(AppError::from)?
};
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_data_types(
@@ -321,8 +324,8 @@ pub async fn list_data_types(
) -> Result<Json<serde_json::Value>, AppError> {
let database = q.database.as_deref().unwrap_or("");
let result =
dbx_core::schema::list_data_types_core(&state.app, &q.connection_id, database).await.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
dbx_core::schema::list_data_types_core(&state.app, &q.connection_id, database).await.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_indexes(
@@ -335,13 +338,13 @@ pub async fn list_indexes(
let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await {
dbx_core::schema::list_doris_catalog_indexes_core(&state.app, &q.connection_id, &catalog, database, table)
.await
.map_err(AppError)?
.map_err(AppError::from)?
} else {
dbx_core::schema::list_indexes_core(&state.app, &q.connection_id, database, schema, table)
.await
.map_err(AppError)?
.map_err(AppError::from)?
};
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_foreign_keys(
@@ -354,13 +357,13 @@ pub async fn list_foreign_keys(
let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await {
dbx_core::schema::list_doris_catalog_foreign_keys_core(&state.app, &q.connection_id, &catalog, database, table)
.await
.map_err(AppError)?
.map_err(AppError::from)?
} else {
dbx_core::schema::list_foreign_keys_core(&state.app, &q.connection_id, database, schema, table)
.await
.map_err(AppError)?
.map_err(AppError::from)?
};
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_triggers(
@@ -373,13 +376,13 @@ pub async fn list_triggers(
let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await {
dbx_core::schema::list_doris_catalog_triggers_core(&state.app, &q.connection_id, &catalog, database, table)
.await
.map_err(AppError)?
.map_err(AppError::from)?
} else {
dbx_core::schema::list_triggers_core(&state.app, &q.connection_id, database, schema, table)
.await
.map_err(AppError)?
.map_err(AppError::from)?
};
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn get_ddl(
@@ -392,11 +395,11 @@ pub async fn get_ddl(
let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await {
dbx_core::schema::get_doris_catalog_table_ddl_core(&state.app, &q.connection_id, &catalog, database, table)
.await
.map_err(AppError)?
.map_err(AppError::from)?
} else {
dbx_core::schema::get_table_ddl_core(&state.app, &q.connection_id, database, schema, table, q.object_type)
.await
.map_err(AppError)?
.map_err(AppError::from)?
};
Ok(Json(result))
}
@@ -409,8 +412,8 @@ pub async fn list_functions(
let schema = q.schema.as_deref().unwrap_or("");
let result = dbx_core::schema::list_functions_core(&state.app, &q.connection_id, database, schema)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
#[derive(Deserialize)]
@@ -435,8 +438,8 @@ pub async fn list_sequences(
q.with_last_values.unwrap_or(false),
)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_rules(
@@ -445,9 +448,10 @@ pub async fn list_rules(
) -> Result<Json<serde_json::Value>, AppError> {
let database = q.database.as_deref().unwrap_or("");
let schema = q.schema.as_deref().unwrap_or("");
let result =
dbx_core::schema::list_rules_core(&state.app, &q.connection_id, database, schema).await.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
let result = dbx_core::schema::list_rules_core(&state.app, &q.connection_id, database, schema)
.await
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_owners(
@@ -456,9 +460,10 @@ pub async fn list_owners(
) -> Result<Json<serde_json::Value>, AppError> {
let database = q.database.as_deref().unwrap_or("");
let schema = q.schema.as_deref().unwrap_or("");
let result =
dbx_core::schema::list_owners_core(&state.app, &q.connection_id, database, schema).await.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
let result = dbx_core::schema::list_owners_core(&state.app, &q.connection_id, database, schema)
.await
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_extensions(
@@ -469,8 +474,8 @@ pub async fn list_extensions(
let schema = q.schema.as_deref().unwrap_or("");
let result = dbx_core::schema::list_extensions_core(&state.app, &q.connection_id, database, schema)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn list_available_extensions(
@@ -480,6 +485,6 @@ pub async fn list_available_extensions(
let database = q.database.as_deref().unwrap_or("");
let result = dbx_core::schema::list_available_extensions_core(&state.app, &q.connection_id, database)
.await
.map_err(AppError)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?))
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
+3 -3
View File
@@ -28,7 +28,7 @@ pub async fn save_schema_cache(
State(state): State<Arc<WebState>>,
Json(body): Json<SaveSchemaCacheRequest>,
) -> Result<Json<()>, AppError> {
state.app.storage.save_schema_cache(&body.cache_key, &body.payload).await.map_err(AppError)?;
state.app.storage.save_schema_cache(&body.cache_key, &body.payload).await.map_err(AppError::from)?;
Ok(Json(()))
}
@@ -36,7 +36,7 @@ pub async fn load_schema_cache(
State(state): State<Arc<WebState>>,
Query(query): Query<SchemaCacheKeyQuery>,
) -> Result<Json<serde_json::Value>, AppError> {
let payload = state.app.storage.load_schema_cache(&query.cache_key).await.map_err(AppError)?;
let payload = state.app.storage.load_schema_cache(&query.cache_key).await.map_err(AppError::from)?;
Ok(Json(payload.unwrap_or(serde_json::json!(null))))
}
@@ -44,6 +44,6 @@ pub async fn delete_schema_cache_prefix(
State(state): State<Arc<WebState>>,
Query(query): Query<SchemaCachePrefixQuery>,
) -> Result<Json<()>, AppError> {
state.app.storage.delete_schema_cache_prefix(&query.prefix).await.map_err(AppError)?;
state.app.storage.delete_schema_cache_prefix(&query.prefix).await.map_err(AppError::from)?;
Ok(Json(()))
}
+15 -15
View File
@@ -35,17 +35,17 @@ pub async fn preview_sql_file(
mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, AppError> {
let tmp_dir = state.data_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?;
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
if let Some(field) = multipart.next_field().await.map_err(|e| AppError(e.to_string()))? {
if let Some(field) = multipart.next_field().await.map_err(|e| AppError::from(e.to_string()))? {
let file_name = field.file_name().unwrap_or("upload.sql").to_string();
let data = field.bytes().await.map_err(|e| AppError(e.to_string()))?;
let data = field.bytes().await.map_err(|e| AppError::from(e.to_string()))?;
let file_path = safe_uploaded_sql_path(&tmp_dir, &file_name)?;
std::fs::write(&file_path, &data).map_err(|e| AppError(e.to_string()))?;
std::fs::write(&file_path, &data).map_err(|e| AppError::from(e.to_string()))?;
let size_bytes = data.len() as u64;
let content = sql::decode_sql_file_bytes(&data).map_err(AppError)?;
let content = sql::decode_sql_file_bytes(&data).map_err(AppError::from)?;
let preview: String = content.chars().take(20_000).collect();
return Ok(Json(serde_json::json!({
@@ -57,7 +57,7 @@ pub async fn preview_sql_file(
})));
}
Err(AppError("No file uploaded".to_string()))
Err(AppError::from("No file uploaded".to_string()))
}
pub async fn execute_sql_file(
@@ -68,7 +68,7 @@ pub async fn execute_sql_file(
// Fast-fail: reject early if the connection is read-only (individual statements are also checked in do_execute)
if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.connection_id).await {
return Err(AppError(format!(
return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. SQL file execution blocked.",
name
)));
@@ -81,7 +81,7 @@ pub async fn execute_sql_file(
{
let mut executions = state.sql_file_executions.write().await;
if executions.contains_key(&execution_id) {
return Err(AppError(format!("SQL file execution '{execution_id}' already exists")));
return Err(AppError::from(format!("SQL file execution '{execution_id}' already exists")));
}
executions.insert(execution_id.clone(), token.clone());
}
@@ -162,7 +162,7 @@ async fn cleanup_sql_file_execution(state: &WebState, execution_id: &str) {
fn safe_uploaded_sql_path(tmp_dir: &Path, file_name: &str) -> Result<PathBuf, AppError> {
let base_name = file_name.rsplit(['/', '\\']).find(|part| !part.is_empty()).unwrap_or("upload.sql").trim();
if base_name.is_empty() || base_name == "." || base_name == ".." {
return Err(AppError("Invalid SQL file name".to_string()));
return Err(AppError::from("Invalid SQL file name".to_string()));
}
Ok(tmp_dir.join(base_name))
}
@@ -170,13 +170,13 @@ fn safe_uploaded_sql_path(tmp_dir: &Path, file_name: &str) -> Result<PathBuf, Ap
fn validated_uploaded_sql_path(data_dir: &Path, file_path: &str) -> Result<PathBuf, AppError> {
let path = PathBuf::from(file_path);
if !path.is_absolute() {
return Err(AppError("File path must be absolute".to_string()));
return Err(AppError::from("File path must be absolute".to_string()));
}
let tmp_dir = data_dir.join("tmp").canonicalize().map_err(|e| AppError(e.to_string()))?;
let canonical_path = path.canonicalize().map_err(|e| AppError(e.to_string()))?;
let tmp_dir = data_dir.join("tmp").canonicalize().map_err(|e| AppError::from(e.to_string()))?;
let canonical_path = path.canonicalize().map_err(|e| AppError::from(e.to_string()))?;
if !canonical_path.starts_with(&tmp_dir) {
return Err(AppError("File path must be inside the uploaded SQL directory".to_string()));
return Err(AppError::from("File path must be inside the uploaded SQL directory".to_string()));
}
Ok(canonical_path)
}
@@ -186,7 +186,7 @@ pub async fn sql_file_progress(
AxumPath(execution_id): AxumPath<String>,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> {
let channels = state.sse_channels.read().await;
let tx = channels.get(&execution_id).ok_or_else(|| AppError("Execution not found".to_string()))?;
let tx = channels.get(&execution_id).ok_or_else(|| AppError::from("Execution not found".to_string()))?;
let rx = tx.subscribe();
drop(channels);
Ok(crate::sse::sse_from_lossy_channel(rx))
@@ -216,7 +216,7 @@ mod tests {
let path = match safe_uploaded_sql_path(&tmp_dir, "../outside.sql") {
Ok(path) => path,
Err(error) => panic!("{}", error.0),
Err(error) => panic!("{}", error.message),
};
assert_eq!(path, tmp_dir.join("outside.sql"));
+6 -6
View File
@@ -39,14 +39,14 @@ pub async fn start_table_export(
// Generate temp file path for web export output
let tmp_dir = state.data_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?;
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
let ext = match req.format.as_str() {
"csv" => "csv",
"xlsx" => "xlsx",
"json" => "json",
"markdown" | "md" => "md",
"sql" => "sql",
_ => return Err(AppError(format!("Unsupported export format: {}", req.format))),
_ => return Err(AppError::from(format!("Unsupported export format: {}", req.format))),
};
let tmp_file = tmp_dir.join(format!("table_export_{export_id}.{ext}"));
let file_path = tmp_file.to_string_lossy().to_string();
@@ -132,9 +132,9 @@ pub async fn table_export_download(
.write()
.await
.remove(&export_id)
.ok_or_else(|| AppError("Export file not found".to_string()))?;
.ok_or_else(|| AppError::from("Export file not found".to_string()))?;
let data = tokio::fs::read(&file_path).await.map_err(|e| AppError(e.to_string()))?;
let data = tokio::fs::read(&file_path).await.map_err(|e| AppError::from(e.to_string()))?;
// Clean up temp file
let _ = tokio::fs::remove_file(&file_path).await;
@@ -144,7 +144,7 @@ pub async fn table_export_download(
"json" => ("application/json; charset=utf-8", "json"),
"markdown" | "md" => ("text/markdown; charset=utf-8", "md"),
"sql" => ("application/sql; charset=utf-8", "sql"),
_ => return Err(AppError(format!("Unknown format: {format}"))),
_ => return Err(AppError::from(format!("Unknown format: {format}"))),
};
let filename = format!("table_export_{export_id}.{file_ext}");
@@ -154,5 +154,5 @@ pub async fn table_export_download(
.header(header::CONTENT_TYPE, content_type)
.header(header::CONTENT_DISPOSITION, format!("attachment; filename=\"{filename}\""))
.body(Body::from(data))
.map_err(|e| AppError(e.to_string()))
.map_err(|e| AppError::from(e.to_string()))
}
+23 -23
View File
@@ -37,7 +37,7 @@ pub async fn preview_import(
mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, AppError> {
let tmp_dir = import_upload_dir(&state.data_dir);
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?;
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
cleanup_expired_import_uploads(&tmp_dir, Duration::from_secs(24 * 60 * 60));
let mut uploaded_file: Option<(String, PathBuf)> = None;
@@ -51,14 +51,14 @@ pub async fn preview_import(
Ok(None) => break,
Err(error) => {
cleanup_pending_upload(&uploaded_file).await;
return Err(AppError(error.to_string()));
return Err(AppError::from(error.to_string()));
}
};
let name = field.name().unwrap_or_default().to_string();
if name == "file" {
if uploaded_file.is_some() {
cleanup_pending_upload(&uploaded_file).await;
return Err(AppError("Only one import file may be uploaded".to_string()));
return Err(AppError::from("Only one import file may be uploaded".to_string()));
}
let file_name = field.file_name().unwrap_or("upload.csv").to_string();
let source_ref = uuid::Uuid::new_v4().to_string();
@@ -73,7 +73,7 @@ pub async fn preview_import(
Ok(value) => value,
Err(error) => {
cleanup_pending_upload(&uploaded_file).await;
return Err(AppError(error.to_string()));
return Err(AppError::from(error.to_string()));
}
};
match name.as_str() {
@@ -82,7 +82,7 @@ pub async fn preview_import(
Ok(source_format) => Some(source_format),
Err(error) => {
cleanup_pending_upload(&uploaded_file).await;
return Err(AppError(error.to_string()));
return Err(AppError::from(error.to_string()));
}
};
}
@@ -91,7 +91,7 @@ pub async fn preview_import(
Ok(parse_options) => parse_options,
Err(error) => {
cleanup_pending_upload(&uploaded_file).await;
return Err(AppError(error.to_string()));
return Err(AppError::from(error.to_string()));
}
};
}
@@ -117,20 +117,20 @@ pub async fn preview_import(
Ok(preview) => preview,
Err(error) => {
cleanup_uploaded_import_path(&file_path).await;
return Err(AppError(error));
return Err(AppError::from(error));
}
};
let preview = match serde_json::to_value(preview) {
Ok(preview) => preview,
Err(error) => {
cleanup_uploaded_import_path(&file_path).await;
return Err(AppError(error.to_string()));
return Err(AppError::from(error.to_string()));
}
};
return Ok(Json(preview));
}
Err(AppError("No file uploaded".to_string()))
Err(AppError::from("No file uploaded".to_string()))
}
async fn write_import_upload(field: axum::extract::multipart::Field<'_>, file_path: &StdPath) -> Result<(), AppError> {
@@ -146,22 +146,22 @@ where
S: Stream<Item = Result<Bytes, E>> + Unpin,
E: std::fmt::Display,
{
let mut upload = tokio::fs::File::create(file_path).await.map_err(|error| AppError(error.to_string()))?;
let mut upload = tokio::fs::File::create(file_path).await.map_err(|error| AppError::from(error.to_string()))?;
let mut uploaded_bytes = 0usize;
// Stream uploads to disk so valid large CSV files don't require a second full-size in-memory copy.
let result = async {
while let Some(chunk) = chunks.next().await {
let chunk = chunk.map_err(|error| AppError(error.to_string()))?;
let chunk = chunk.map_err(|error| AppError::from(error.to_string()))?;
uploaded_bytes = uploaded_bytes.saturating_add(chunk.len());
if uploaded_bytes > max_upload_bytes {
return Err(AppError(format!(
return Err(AppError::from(format!(
"File too large: {uploaded_bytes} bytes received (max {max_upload_bytes} bytes)"
)));
}
upload.write_all(&chunk).await.map_err(|error| AppError(error.to_string()))?;
upload.write_all(&chunk).await.map_err(|error| AppError::from(error.to_string()))?;
}
upload.flush().await.map_err(|error| AppError(error.to_string()))
upload.flush().await.map_err(|error| AppError::from(error.to_string()))
}
.await;
drop(upload);
@@ -183,7 +183,7 @@ pub async fn execute_import(
// Reject import early if the connection is read-only
if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.connection_id).await {
cleanup_uploaded_import_source(&req.file_path).await;
return Err(AppError(format!(
return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. Import blocked.",
name
)));
@@ -287,7 +287,7 @@ pub async fn import_progress(
Path(import_id): Path<String>,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> {
let channels = state.sse_channels.read().await;
let tx = channels.get(&import_id).ok_or_else(|| AppError("Import not found".to_string()))?;
let tx = channels.get(&import_id).ok_or_else(|| AppError::from("Import not found".to_string()))?;
let rx = tx.subscribe();
drop(channels);
Ok(crate::sse::sse_from_channel(rx))
@@ -308,7 +308,7 @@ fn import_upload_dir(data_dir: &StdPath) -> PathBuf {
fn safe_uploaded_import_path(tmp_dir: &StdPath, file_name: &str, source_ref: &str) -> Result<PathBuf, AppError> {
let base_name = file_name.rsplit(['/', '\\']).find(|part| !part.is_empty()).unwrap_or("upload.csv").trim();
if base_name.is_empty() || base_name == "." || base_name == ".." {
return Err(AppError("Invalid import file name".to_string()));
return Err(AppError::from("Invalid import file name".to_string()));
}
Ok(tmp_dir.join(format!("{source_ref}-{base_name}")))
}
@@ -316,14 +316,14 @@ fn safe_uploaded_import_path(tmp_dir: &StdPath, file_name: &str, source_ref: &st
fn validated_uploaded_import_path(data_dir: &StdPath, file_path: &str) -> Result<PathBuf, AppError> {
let path = PathBuf::from(file_path);
if !path.is_absolute() {
return Err(AppError("Import source path must be absolute".to_string()));
return Err(AppError::from("Import source path must be absolute".to_string()));
}
let tmp_dir = import_upload_dir(data_dir).canonicalize().map_err(|e| AppError(e.to_string()))?;
let tmp_dir = import_upload_dir(data_dir).canonicalize().map_err(|e| AppError::from(e.to_string()))?;
let canonical_path =
path.canonicalize().map_err(|e| AppError(format!("Import source is no longer available: {e}")))?;
path.canonicalize().map_err(|e| AppError::from(format!("Import source is no longer available: {e}")))?;
if !canonical_path.starts_with(&tmp_dir) {
return Err(AppError("Import source must be inside the uploaded import directory".to_string()));
return Err(AppError::from("Import source must be inside the uploaded import directory".to_string()));
}
Ok(canonical_path)
}
@@ -387,7 +387,7 @@ mod tests {
let error = write_import_upload_stream(chunks, &file_path, 4).await.unwrap_err();
assert!(error.0.contains("File too large"));
assert!(error.message.contains("File too large"));
assert!(!file_path.exists());
}
@@ -398,7 +398,7 @@ mod tests {
let error = write_import_upload_stream(chunks, &file_path, 8).await.unwrap_err();
assert_eq!(error.0, "multipart stream failed");
assert_eq!(error.message, "multipart stream failed");
assert!(!file_path.exists());
}
}
+18 -12
View File
@@ -33,11 +33,11 @@ pub async fn start_transfer(
Json(body): Json<StartTransferRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
let req = body.request;
transfer::validate_transfer_target_table_names(&req).map_err(AppError)?;
transfer::validate_transfer_target_table_names(&req).map_err(AppError::from)?;
// Reject transfer early if the target connection is read-only
if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.target_connection_id).await {
return Err(AppError(format!(
return Err(AppError::from(format!(
"Read-only mode: target connection '{}' has read-only protection enabled. Transfer blocked.",
name
)));
@@ -353,13 +353,19 @@ pub async fn preview_transfer_ownership(
Json(body): Json<PreviewTransferOwnershipRequest>,
) -> Result<Json<dbx_core::transfer::TransferOwnershipPreview>, AppError> {
let req = body.request;
transfer::validate_transfer_target_table_names(&req).map_err(AppError)?;
let source_db_type = transfer::get_db_type(&state.app, &req.source_connection_id).await.map_err(AppError)?;
let target_db_type = transfer::get_db_type(&state.app, &req.target_connection_id).await.map_err(AppError)?;
let source_pool_key =
state.app.get_or_create_pool(&req.source_connection_id, Some(&req.source_database)).await.map_err(AppError)?;
let target_pool_key =
state.app.get_or_create_pool(&req.target_connection_id, Some(&req.target_database)).await.map_err(AppError)?;
transfer::validate_transfer_target_table_names(&req).map_err(AppError::from)?;
let source_db_type = transfer::get_db_type(&state.app, &req.source_connection_id).await.map_err(AppError::from)?;
let target_db_type = transfer::get_db_type(&state.app, &req.target_connection_id).await.map_err(AppError::from)?;
let source_pool_key = state
.app
.get_or_create_pool(&req.source_connection_id, Some(&req.source_database))
.await
.map_err(AppError::from)?;
let target_pool_key = state
.app
.get_or_create_pool(&req.target_connection_id, Some(&req.target_database))
.await
.map_err(AppError::from)?;
let preview = transfer::preview_transfer_ownership(
&state.app,
&req,
@@ -369,7 +375,7 @@ pub async fn preview_transfer_ownership(
&target_pool_key,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(preview))
}
@@ -378,7 +384,7 @@ pub async fn transfer_progress(
Path(transfer_id): Path<String>,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> {
let channels = state.sse_channels.read().await;
let tx = channels.get(&transfer_id).ok_or_else(|| AppError("Transfer not found".to_string()))?;
let tx = channels.get(&transfer_id).ok_or_else(|| AppError::from("Transfer not found".to_string()))?;
let rx = tx.subscribe();
drop(channels);
Ok(crate::sse::sse_from_channel(rx))
@@ -416,5 +422,5 @@ pub async fn sort_tables_by_fk_dependency(
)
.await
.map(Json)
.map_err(AppError)
.map_err(AppError::from)
}
+3 -3
View File
@@ -17,19 +17,19 @@ pub struct SaveTunnelProfilesRequest {
pub async fn load_tunnel_profiles(
State(state): State<Arc<WebState>>,
) -> Result<Json<Vec<TransportLayerConfig>>, AppError> {
state.app.storage.load_tunnel_profiles().await.map(Json).map_err(AppError)
state.app.storage.load_tunnel_profiles().await.map(Json).map_err(AppError::from)
}
pub async fn save_tunnel_profiles(
State(state): State<Arc<WebState>>,
Json(body): Json<SaveTunnelProfilesRequest>,
) -> Result<Json<()>, AppError> {
state.app.storage.save_tunnel_profiles(&body.profiles).await.map(Json).map_err(AppError)
state.app.storage.save_tunnel_profiles(&body.profiles).await.map(Json).map_err(AppError::from)
}
pub async fn test_tunnel_profile(
State(state): State<Arc<WebState>>,
Json(profile): Json<TransportLayerConfig>,
) -> Result<Json<String>, AppError> {
state.app.test_tunnel_profile(&profile).await.map(Json).map_err(AppError)
state.app.test_tunnel_profile(&profile).await.map(Json).map_err(AppError::from)
}
+4 -3
View File
@@ -17,9 +17,10 @@ pub struct UpdateCheckParams {
pub async fn check_for_updates(Query(params): Query<UpdateCheckParams>) -> Result<Json<serde_json::Value>, AppError> {
let locale = params.locale.unwrap_or_else(|| "zh-CN".to_string());
let release = update::fetch_latest_release(&locale, params.source.unwrap_or_default()).await.map_err(AppError)?;
let release =
update::fetch_latest_release(&locale, params.source.unwrap_or_default()).await.map_err(AppError::from)?;
let info = update::build_update_info(release, env!("CARGO_PKG_VERSION"));
Ok(Json(serde_json::to_value(info).map_err(|e| AppError(e.to_string()))?))
Ok(Json(serde_json::to_value(info).map_err(|e| AppError::from(e.to_string()))?))
}
#[derive(serde::Deserialize)]
@@ -32,6 +33,6 @@ pub async fn fetch_changelog(
Query(params): Query<ChangelogParams>,
) -> Result<Json<changelog::ChangelogData>, AppError> {
let lang = params.lang.unwrap_or_else(|| "en".to_string());
let data = changelog::fetch_changelog(&lang).await.map_err(AppError)?;
let data = changelog::fetch_changelog(&lang).await.map_err(AppError::from)?;
Ok(Json(data))
}
+6 -5
View File
@@ -13,7 +13,7 @@ async fn ensure_writable(
action: &str,
) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
return Err(AppError(format!(
return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
name, action
)));
@@ -60,7 +60,7 @@ pub async fn list_prefix(
req.recursive,
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -68,7 +68,8 @@ pub async fn get(
State(state): State<Arc<WebState>>,
Json(req): Json<ZooKeeperKeyRequest>,
) -> Result<Json<dbx_core::agent_kv::KvGetResponse>, AppError> {
let result = dbx_core::agent_kv::kv_get_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError)?;
let result =
dbx_core::agent_kv::kv_get_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -85,7 +86,7 @@ pub async fn put(
req.options.unwrap_or_default(),
)
.await
.map_err(AppError)?;
.map_err(AppError::from)?;
Ok(Json(result))
}
@@ -95,6 +96,6 @@ pub async fn delete(
) -> Result<Json<dbx_core::agent_kv::KvDeleteResponse>, AppError> {
ensure_writable(&state.app, &req.connection_id, "Delete").await?;
let result =
dbx_core::agent_kv::kv_delete_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError)?;
dbx_core::agent_kv::kv_delete_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError::from)?;
Ok(Json(result))
}