fix: expose InvalidJSONFieldError and NoFormData in multipartErrors#623
Open
LeSingh1 wants to merge 1 commit into
Open
fix: expose InvalidJSONFieldError and NoFormData in multipartErrors#623LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
Two error constructors defined in the plugin were not included in the fastify.multipartErrors decorator: InvalidJSONFieldError (thrown when a field declares application/json content-type but carries invalid JSON) and NoFormData (thrown when FormData is unavailable in the runtime). Without this fix, users calling `error instanceof fastify.multipartErrors.InvalidJSONFieldError` would receive a TypeError because the property was undefined, even though the error code FST_INVALID_JSON_FIELD_ERROR is part of the documented API surface. Also adds the missing FileBufferNotFoundError, InvalidJSONFieldError, and NoFormData entries to the MultipartErrors TypeScript interface so that the type and runtime are in sync. Fixes the latent wrong t.plan counts in two existing tests that were silently passing only because the instanceof check threw a TypeError and skipped one assertion.
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.
What
Two error constructors defined inside the plugin were missing from the
fastify.multipartErrorsdecorator:InvalidJSONFieldError(codeFST_INVALID_JSON_FIELD_ERROR) — thrown when a multipart field hasContent-Type: application/jsonbut carries invalid JSONNoFormData(codeFST_NO_FORM_DATA) — thrown whenglobalThis.FormDatais unavailableThe TypeScript
MultipartErrorsinterface was also missingFileBufferNotFoundError,InvalidJSONFieldError, andNoFormData, leaving the type out of sync with the runtime.Why it matters
Users who want to branch on the error type idiomatically do:
With
InvalidJSONFieldErrorabsent frommultipartErrors, that check throws aTypeError: Right-hand side of 'instanceof' is not callableinstead of returningfalse, which is a silent breakage. The error is thrown by the plugin with the documented code, but there was no way to reach its constructor through the public API.What changed
index.js: addInvalidJSONFieldErrorandNoFormDatato thefastify.decorate('multipartErrors', …)calltypes/index.d.ts: addFileBufferNotFoundError,InvalidJSONFieldError, andNoFormDatatoMultipartErrorstest/multipart-json.test.js:t.plan(2)→t.plan(3)in two existing tests that were silently miscounted (the instanceof check was throwing a TypeError, skipping one assertion, and the wrong plan count went unnoticed)multipartErrors.InvalidJSONFieldErroris a function and that an error thrown by the plugin is an instance of itAll tests pass (
npm test) including TypeScript type checks.