Changesets · Branches · Tags · Compare
| Changeset | a5cb6ee00a00e105b2f9f6de6ab599548ed15c5063f40ee64acee74c53ce1da7 |
|---|---|
| Author | WaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-03 05:53 +08:00 |
| Committer | GitHub <noreply@github.com> · 2026-09-03 05:53 +08:00 |
| Parents | 6085661b1298 |
1 file changed
modified crates/utopia-server/src/api/sources_routes.rs +41 −10
| @@ -285,6 +285,10 @@ | ||
| 285 | 285 | #[derive(Deserialize)] |
| 286 | 286 | pub struct IngestBody { |
| 287 | 287 | pub filename: String, |
| 288 | + /// 墓碑推送(`deleted: true`)可以不带 content——指南一直这么写,而字段 | |
| 289 | + /// 从前是必填,缺了就在反序列化那一步被拒。其余情况仍然必填:空串在 | |
| 290 | + /// 下面的校验里挡 | |
| 291 | + #[serde(default)] | |
| 288 | 292 | pub content: String, |
| 289 | 293 | #[serde(default)] |
| 290 | 294 | pub doc_time: Option<DateTime<Utc>>, |
| @@ -341,14 +345,33 @@ | ||
| 341 | 345 | Ok(Json(json!({ "action": action_str(action) }))) |
| 342 | 346 | } |
| 343 | 347 | |
| 348 | +/// 推送失败的两种性质。**调用方发错了**和**我们这边没接住**得分开:前者记进 | |
| 349 | +/// run 供集成调试,但不算来源同步失败——来源没坏,是那一次请求不合格; | |
| 350 | +/// 后者才该把来源标成 failed 并进告警中心。从前两者都走 `finish_sync(error)`, | |
| 351 | +/// 一次格式错误就让铃铛说"来源同步失败,没有新内容进来" | |
| 352 | +enum PushError { | |
| 353 | + /// 4xx:负载不合格(JSON 解析、缺字段) | |
| 354 | + Rejected(String), | |
| 355 | + /// 摄入本身失败 | |
| 356 | + Failed(String), | |
| 357 | +} | |
| 358 | + | |
| 359 | +impl PushError { | |
| 360 | + fn message(&self) -> &str { | |
| 361 | + match self { | |
| 362 | + PushError::Rejected(m) | PushError::Failed(m) => m, | |
| 363 | + } | |
| 364 | + } | |
| 365 | +} | |
| 366 | + | |
| 344 | 367 | /// 认证之后的推送处理:解析 + 校验 + 摄入/墓碑。错误一律返回文字(记进 run)。 |
| 345 | 368 | async fn handle_push( |
| 346 | 369 | state: &AppState, |
| 347 | 370 | source: &utopia_core::models::Source, |
| 348 | 371 | bytes: &[u8], |
| 349 | -) -> Result<crate::ingest_sources::IngestAction, String> { | |
| 350 | - let body: IngestBody = | |
| 351 | - serde_json::from_slice(bytes).map_err(|e| format!("Invalid JSON payload: {e}"))?; | |
| 372 | +) -> Result<crate::ingest_sources::IngestAction, PushError> { | |
| 373 | + let body: IngestBody = serde_json::from_slice(bytes) | |
| 374 | + .map_err(|e| PushError::Rejected(format!("Invalid JSON payload: {e}")))?; | |
| 352 | 375 | let identity = body |
| 353 | 376 | .external_id |
| 354 | 377 | .as_deref() |
| @@ -357,7 +380,9 @@ | ||
| 357 | 380 | .unwrap_or_else(|| body.filename.trim()) |
| 358 | 381 | .to_string(); |
| 359 | 382 | if identity.is_empty() { |
| 360 | - return Err("external_id or filename is required".into()); | |
| 383 | + return Err(PushError::Rejected( | |
| 384 | + "external_id or filename is required".into(), | |
| 385 | + )); | |
| 361 | 386 | } |
| 362 | 387 | let key = format!("api:{identity}"); |
| 363 | 388 | |
| @@ -365,12 +390,14 @@ | ||
| 365 | 390 | if body.deleted { |
| 366 | 391 | utopia_store::documents::mark_missing_keys(&state.pool, source.id, &[key]) |
| 367 | 392 | .await |
| 368 | - .map_err(|e| e.to_string())?; | |
| 393 | + .map_err(|e| PushError::Failed(e.to_string()))?; | |
| 369 | 394 | return Ok(crate::ingest_sources::IngestAction::Tombstoned); |
| 370 | 395 | } |
| 371 | 396 | |
| 372 | 397 | if body.filename.trim().is_empty() || body.content.trim().is_empty() { |
| 373 | - return Err("filename and content are required".into()); | |
| 398 | + return Err(PushError::Rejected( | |
| 399 | + "filename and content are required".into(), | |
| 400 | + )); | |
| 374 | 401 | } |
| 375 | 402 | let action = crate::ingest_sources::ingest_item( |
| 376 | 403 | state, |
| @@ -383,11 +410,11 @@ | ||
| 383 | 410 | body.doc_time, |
| 384 | 411 | ) |
| 385 | 412 | .await |
| 386 | - .map_err(|e| e.to_string())?; | |
| 413 | + .map_err(|e| PushError::Failed(e.to_string()))?; | |
| 387 | 414 | // 失而复得:曾被墓碑标记的身份再次正常推送,摘掉 missing 标记 |
| 388 | 415 | utopia_store::documents::clear_missing_keys(&state.pool, source.id, &[key]) |
| 389 | 416 | .await |
| 390 | - .map_err(|e| e.to_string())?; | |
| 417 | + .map_err(|e| PushError::Failed(e.to_string()))?; | |
| 391 | 418 | Ok(action) |
| 392 | 419 | } |
| 393 | 420 | |
| @@ -434,10 +461,14 @@ | ||
| 434 | 461 | state.emit_source(source.kb_id); |
| 435 | 462 | Ok(Json(json!({ "action": action_str(action) }))) |
| 436 | 463 | } |
| 437 | - Err(msg) => { | |
| 464 | + Err(err) => { | |
| 465 | + let msg = err.message().to_string(); | |
| 438 | 466 | utopia_store::sources::finish_run(&state.pool, run, source.id, Some(&msg), 0, 0) |
| 439 | 467 | .await?; |
| 440 | - utopia_store::sources::finish_sync(&state.pool, source.id, Some(&msg), 0).await?; | |
| 468 | + // 只有我们这边没接住才算来源失败;调用方发错了留在 run 历史里就够 | |
| 469 | + if let PushError::Failed(_) = err { | |
| 470 | + utopia_store::sources::finish_sync(&state.pool, source.id, Some(&msg), 0).await?; | |
| 471 | + } | |
| 441 | 472 | state.emit_source(source.kb_id); |
| 442 | 473 | Err(utopia_core::AppError::Validation(msg).into()) |
| 443 | 474 | } |