Skip to content

fix: expose InvalidJSONFieldError and NoFormData in multipartErrors#623

Open
LeSingh1 wants to merge 1 commit into
fastify:mainfrom
LeSingh1:fix/multipart-errors-expose-all-constructors
Open

fix: expose InvalidJSONFieldError and NoFormData in multipartErrors#623
LeSingh1 wants to merge 1 commit into
fastify:mainfrom
LeSingh1:fix/multipart-errors-expose-all-constructors

Conversation

@LeSingh1

Copy link
Copy Markdown

What

Two error constructors defined inside the plugin were missing from the fastify.multipartErrors decorator:

  • InvalidJSONFieldError (code FST_INVALID_JSON_FIELD_ERROR) — thrown when a multipart field has Content-Type: application/json but carries invalid JSON
  • NoFormData (code FST_NO_FORM_DATA) — thrown when globalThis.FormData is unavailable

The TypeScript MultipartErrors interface was also missing FileBufferNotFoundError, InvalidJSONFieldError, and NoFormData, leaving the type out of sync with the runtime.

Why it matters

Users who want to branch on the error type idiomatically do:

} catch (err) {
  if (err instanceof fastify.multipartErrors.InvalidJSONFieldError) { ... }
}

With InvalidJSONFieldError absent from multipartErrors, that check throws a TypeError: Right-hand side of 'instanceof' is not callable instead of returning false, 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: add InvalidJSONFieldError and NoFormData to the fastify.decorate('multipartErrors', …) call
  • types/index.d.ts: add FileBufferNotFoundError, InvalidJSONFieldError, and NoFormData to MultipartErrors
  • test/multipart-json.test.js:
    • Fix 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)
    • Add a unit test confirming multipartErrors.InvalidJSONFieldError is a function and that an error thrown by the plugin is an instance of it

All tests pass (npm test) including TypeScript type checks.

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.
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