Repository navigation
fix(knowledge): answer 400 for an unsupported upload type - #5877
Open
shunxinyi-ljs wants to merge 1 commit into
Open
shunxinyi-ljs wants to merge 1 commit into
shunxinyi-ljs wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
POST /api/v1/knowledge-bases/:id/knowledge/fileanswers 500 for a file whoseextension is unsupported. The service rejects it with
ErrInvalidFileType, but thatsentinel is a plain
errors.New; the handler forwards only*AppErrorunchanged(
internal/handler/knowledge.go:438-448) and wraps everything else inNewInternalServerError.Make the sentinel a 400-class
AppError(
werrors.NewBadRequestError("unsupported file type")), so the rejection reaches clientsas 400. Callers that match it with
errors.Is(datasource sync, IM) keep working becausethe sentinel variable itself is unchanged.
Type of Change
Related Issue
None.
Testing
TestCreateKnowledgeFromFileRejectsUnsupportedTypeposts a multipart.exeuploadthrough the real handler and error middleware. On the parent commit it returns 500
(body
code: 1007); with the patch it returns 400 withunsupported file type.TestCreateKnowledgeFromFileAcceptsSupportedTypeposts.mdand still gets 200, sothe type check does not reject valid uploads.
go test ./internal/handler -run TestCreateKnowledgeFromFile— pass.go vet ./internal/handlerandgofmt -l— clean.go build ./...(needs a cgo vector-store build this machine cannotfinish) 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...HEADpassesgolangci-lint run --new-from-rev=origin/main ./...) — golangci-lint is unavailable in this environmentwebsite-docs/, Swagger annotations, etc.) — not applicable