Repository navigation
fix(go): redact internal handler errors - #15746
xugangqiang merged 11 commits into
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds a centralized helper ChangesError Response Redaction
Sequence DiagramsequenceDiagram
participant Handler
participant jsonInternalError
participant commonWarn
Handler->>jsonInternalError: jsonInternalError(c, err)
jsonInternalError->>commonWarn: common.Warn(method, path, err)
jsonInternalError->>Handler: jsonError(common.CodeServerError, common.CodeServerError.Message())
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Hi @xugangqiang would you please review the PR and share your feedback? |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
internal/handler/error_response_test.go (1)
56-58: 💤 Low valueConsider caching the response body string.
recorder.Body.String()is called twice in the conditional. Caching it would improve readability and avoid the redundant call.♻️ Optional refactor
- if strings.Contains(recorder.Body.String(), "password=secret") || strings.Contains(recorder.Body.String(), "10.0.0.1") { - t.Fatalf("response leaked raw error details: %s", recorder.Body.String()) + bodyStr := recorder.Body.String() + if strings.Contains(bodyStr, "password=secret") || strings.Contains(bodyStr, "10.0.0.1") { + t.Fatalf("response leaked raw error details: %s", bodyStr) }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/handler/error_response_test.go` around lines 56 - 58, Cache the response body into a local variable once instead of calling recorder.Body.String() multiple times: e.g., assign recorder.Body.String() to a variable (like respBody) and then use respBody in the conditional and in the t.Fatalf call so you avoid redundant calls and improve readability, updating the code around the existing check that references recorder.Body.String().internal/handler/kb.go (1)
63-72: ⚡ Quick winConsider adding a nil check for the error parameter.
While production code paths should always pass a non-nil error, adding a defensive check would prevent confusing log entries if
jsonInternalErroris accidentally called withnil.🛡️ Suggested defensive check
func jsonInternalError(c *gin.Context, err error) { + if err == nil { + err = errors.New("unknown error") + } common.Warn("handler internal error", zap.Error(err), zap.String("method", c.Request.Method), zap.String("path", c.Request.URL.Path), ) jsonError(c, common.CodeServerError, common.CodeServerError.Message()) }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/handler/kb.go` around lines 63 - 72, jsonInternalError currently assumes err is non-nil; add a defensive nil check at the start of jsonInternalError (function name: jsonInternalError, params: c *gin.Context, err error) to avoid logging confusing nil errors: if err is nil, log a clear message (e.g., "internal error: nil error passed") via common.Warn with method/path context and then call jsonError(c, common.CodeServerError, common.CodeServerError.Message()); otherwise proceed to log zap.Error(err) as before and return the same generic JSON error.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/handler/error_response_test.go`:
- Around line 56-58: Cache the response body into a local variable once instead
of calling recorder.Body.String() multiple times: e.g., assign
recorder.Body.String() to a variable (like respBody) and then use respBody in
the conditional and in the t.Fatalf call so you avoid redundant calls and
improve readability, updating the code around the existing check that references
recorder.Body.String().
In `@internal/handler/kb.go`:
- Around line 63-72: jsonInternalError currently assumes err is non-nil; add a
defensive nil check at the start of jsonInternalError (function name:
jsonInternalError, params: c *gin.Context, err error) to avoid logging confusing
nil errors: if err is nil, log a clear message (e.g., "internal error: nil error
passed") via common.Warn with method/path context and then call jsonError(c,
common.CodeServerError, common.CodeServerError.Message()); otherwise proceed to
log zap.Error(err) as before and return the same generic JSON error.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: a25bfdb0-19ee-49fc-af4c-7bce4d0de635
📒 Files selected for processing (6)
internal/handler/agent.gointernal/handler/datasets.gointernal/handler/error_response_test.gointernal/handler/file.gointernal/handler/kb.gointernal/handler/system.go
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15746 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/handler/agent.go`:
- Around line 174-180: Replace the direct exposure of internal errors (sending
err.Error() in the JSON response) with the centralized redaction helper
jsonInternalError in the three error blocks in internal/handler/agent.go;
specifically, update the error branches inside CreateAgentSessionItem,
UpdateAgentSessionItem and DeleteAgentSessionItem to call jsonInternalError(c,
code) (or the existing jsonInternalError signature used elsewhere) instead of
embedding err.Error(), and for DeleteAgentSessionItem ensure the response
follows the contract by returning data: false where the JSON contract expects
that branch.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 42c8c5e8-23bc-4703-a2aa-a7166157a464
📒 Files selected for processing (2)
internal/handler/agent.gointernal/handler/datasets.go
🚧 Files skipped from review as they are similar to previous changes (1)
- internal/handler/datasets.go
There was a problem hiding this comment.
Caution
Inline review comments failed to post. This is likely due to GitHub's internal server error or limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/handler/agent.go`:
- Around line 174-180: Replace the direct exposure of internal errors (sending
err.Error() in the JSON response) with the centralized redaction helper
jsonInternalError in the three error blocks in internal/handler/agent.go;
specifically, update the error branches inside CreateAgentSessionItem,
UpdateAgentSessionItem and DeleteAgentSessionItem to call jsonInternalError(c,
code) (or the existing jsonInternalError signature used elsewhere) instead of
embedding err.Error(), and for DeleteAgentSessionItem ensure the response
follows the contract by returning data: false where the JSON contract expects
that branch.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 42c8c5e8-23bc-4703-a2aa-a7166157a464
📒 Files selected for processing (2)
internal/handler/agent.gointernal/handler/datasets.go
🚧 Files skipped from review as they are similar to previous changes (1)
- internal/handler/datasets.go
🛑 Comments failed to post (1)
internal/handler/agent.go (1)
174-180:
⚠️ Potential issue | 🟠 Major | ⚡ Quick winUse centralized redaction for internal session-handler failures.
Line 174, Line 245, and Line 294 still send raw
err.Error()to clients. That can expose internal backend details and bypasses thejsonInternalErrorredaction contract this PR is introducing.Suggested patch pattern
if err != nil { - c.JSON(http.StatusOK, gin.H{ - "code": code, - "data": nil, - "message": err.Error(), - }) + if code == common.CodeServerError { + jsonInternalError(c, err) + return + } + jsonError(c, code, err.Error()) return }Apply the same pattern to each of the three error blocks (use
data: falsebranch similarly inDeleteAgentSessionItemif needed by response contract).Also applies to: 245-251, 294-300
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/handler/agent.go` around lines 174 - 180, Replace the direct exposure of internal errors (sending err.Error() in the JSON response) with the centralized redaction helper jsonInternalError in the three error blocks in internal/handler/agent.go; specifically, update the error branches inside CreateAgentSessionItem, UpdateAgentSessionItem and DeleteAgentSessionItem to call jsonInternalError(c, code) (or the existing jsonInternalError signature used elsewhere) instead of embedding err.Error(), and for DeleteAgentSessionItem ensure the response follows the contract by returning data: false where the JSON contract expects that branch.
|
Hi @wangq8, @JinHai-CN, @yingfeng, would you please review my PR and give me your feedback? |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/handler/error.go (1)
30-30: 💤 Low valueConsider adding nil error guard.
While
zap.Error(nil)is safe (logserror=null), a nil error suggests incorrect usage of this function. Consider adding a defensive check or documenting that callers must pass a non-nil error.🛡️ Optional defensive guard
func jsonInternalError(c *gin.Context, err error) { + if err == nil { + err = fmt.Errorf("jsonInternalError called with nil error") + } common.Warn("handler internal error",🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/handler/error.go` at line 30, The jsonInternalError function should defensively handle a nil err to avoid signaling incorrect usage; inside jsonInternalError check if err == nil and replace it with a clear sentinel error (e.g., errors.New("internal error: missing error") or "unknown error") and/or log a warning before continuing so zap.Error is never passed nil; update jsonInternalError to perform this nil-guard and ensure callers remain unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/handler/error.go`:
- Line 30: The jsonInternalError function should defensively handle a nil err to
avoid signaling incorrect usage; inside jsonInternalError check if err == nil
and replace it with a clear sentinel error (e.g., errors.New("internal error:
missing error") or "unknown error") and/or log a warning before continuing so
zap.Error is never passed nil; update jsonInternalError to perform this
nil-guard and ensure callers remain unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 06eefacd-51a5-4dc2-9d89-a0342e2df4eb
📒 Files selected for processing (2)
internal/handler/error.gointernal/handler/kb.go
💤 Files with no reviewable changes (1)
- internal/handler/kb.go
|
Hi @yingfeng would you please review my PR? |
…nfiniflow#15715) Resolve conflicts from main's ingestion refactor (infiniflow#15327) and handler-error redaction (infiniflow#15746): union the documentServiceIface upload* and *IngestionTasks methods, adopt main's renamed ingestionTaskDAO/ingestionTaskLogDAO while keeping fileDAO, and dedupe the internal/server import. Build, vet, gofmt and service/handler/dao/router tests (incl. upload-linkage regression tests) pass.
What problem does this PR solve?
Refs #15743
Some Go API handlers return raw
err.Error()strings inCodeServerErrorresponses. Those errors can include internal backend details such as database, storage, search engine, or host information.This PR adds a small shared
jsonInternalErrorhelper for handler-level internal failures. The helper logs the raw error server-side with request method/path context, then returns the existing genericcommon.CodeServerError.Message()to API clients.This first slice migrates the existing
jsonError(c, common.CodeServerError, err.Error())production call sites in agent, dataset graph, file, and system handlers. It intentionally does not close the full issue because directc.JSONerror responses in other handlers remain for follow-up PRs.Type of change
Tests
/root/go/bin/go test ./internal/handler -count=1