Skip to content

fix: expose InvalidJSONFieldError and NoFormData in multipartErrors - #623

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

fix: expose InvalidJSONFieldError and NoFormData in multipartErrors#623
LeSingh1 wants to merge 2 commits 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.

@Eomm Eomm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind to rebase this PR?

@LeSingh1
LeSingh1 force-pushed the fix/multipart-errors-expose-all-constructors branch from 4823dd5 to 51387ea Compare July 10, 2026 14:37
Comment thread types/index.d.ts
FileBufferNotFoundError: FastifyErrorConstructor;
PrematureCloseError: FastifyErrorConstructor;
InvalidJSONFieldError: FastifyErrorConstructor;
NoFormData: FastifyErrorConstructor;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add these to the type tests?

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.

3 participants