Skip to content

fix(knowledge): answer 400 for an unsupported upload type - #5877

Open
shunxinyi-ljs wants to merge 1 commit into
Tencent:mainfrom
shunxinyi-ljs:fix/upload-unsupported-type-400
Open

shunxinyi-ljs wants to merge 1 commit into
Tencent:mainfrom
shunxinyi-ljs:fix/upload-unsupported-type-400

Conversation

@shunxinyi-ljs

Copy link
Copy Markdown
Contributor

Description

POST /api/v1/knowledge-bases/:id/knowledge/file answers 500 for a file whose
extension is unsupported. The service rejects it with ErrInvalidFileType, but that
sentinel is a plain errors.New; the handler forwards only *AppError unchanged
(internal/handler/knowledge.go:438-448) and wraps everything else in
NewInternalServerError.

Make the sentinel a 400-class AppError
(werrors.NewBadRequestError("unsupported file type")), so the rejection reaches clients
as 400. Callers that match it with errors.Is (datasource sync, IM) keep working because
the sentinel variable itself is unchanged.

Type of Change

  • 🐛 Bug fix
  • 🧪 Test

Related Issue

None.

Testing

  • TestCreateKnowledgeFromFileRejectsUnsupportedType posts a multipart .exe upload
    through the real handler and error middleware. On the parent commit it returns 500
    (body code: 1007); with the patch it returns 400 with unsupported file type.
  • TestCreateKnowledgeFromFileAcceptsSupportedType posts .md and still gets 200, so
    the type check does not reject valid uploads.
  • go test ./internal/handler -run TestCreateKnowledgeFromFile — pass.
  • go vet ./internal/handler and gofmt -l — clean.
  • Not run here: full go build ./... (needs a cgo vector-store build this machine cannot
    finish) and golangci-lint (not installed).

Behavior change

An unsupported upload type now returns HTTP 400 instead of 500, with the same message.
No route, payload, config key or success-path change.

Checklist

  • git diff --check origin/main...HEAD passes
  • Changed source files are formatted
  • Targeted tests for the changed packages/components pass
  • Diff-scoped lint passes where applicable (for Go: golangci-lint run --new-from-rev=origin/main ./...) — golangci-lint is unavailable in this environment
  • Full-repository checks were run, or any unrelated/environment-dependent failures are documented above
  • Self-reviewed the code
  • Added/updated tests covering the change
  • Updated related documentation (README, website-docs/, Swagger annotations, etc.) — not applicable
  • Breaking changes are clearly called out in the description above (none)

Upload handlers forward every non-AppError from the service layer to the HTTP
layer as 500, while CreateKnowledgeFromFile reports an unsupported extension as
the plain sentinel ErrInvalidFileType. A POST to
/api/v1/knowledge-bases/:id/knowledge/file with a payload.exe therefore answered
HTTP 500 (code 1007), although the same sentinel is already treated as a client
error by the datasource paths.

Make the sentinel an AppError so the existing handler branch returns 400 with
the same message. errors.Is(err, ErrInvalidFileType) keeps working because the
pointer identity is preserved through %w wrapping.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant