feat(middleware): intercept body-parse errors with structured logging - #306
Merged
Merged
Conversation
…accesslayerorg#288) Adds bodyParseErrorMiddleware to src/middlewares/body-parse-error.middleware.ts. - Catches SyntaxError (malformed JSON) and entity.too.large errors produced by express.json() exclusively on mutation methods (POST, PUT, PATCH, DELETE) - Emits a logger.error entry with: type, method, path, requestId, clientIp, errorType — raw request body and headers are never logged - Returns 400 / 413 JSON to the client; non-parse errors and non-mutation methods pass straight through to next() - Mounted in app.ts immediately after express.json() so parse errors are caught before any route or the global error handler sees them - Depends on getClientIp from client-ip.utils (included here as a peer dependency; will resolve cleanly once accesslayerorg#289 is merged) - 14 tests: all mutation verbs, GET/HEAD passthrough, generic errors, entity.too.large branch, no-body-in-log assertion, client response shape Closes accesslayerorg#288
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.
Fixes #288
What changed
src/middlewares/body-parse-error.middleware.ts—bodyParseErrorMiddlewareerror-handler that interceptsexpress.json()parse failures (SyntaxErrorwith.bodyandentity.too.large) on mutation methods only (POST, PUT, PATCH, DELETE)logger.errorwith structured fields:type,method,path,requestId,clientIp,errorType— the raw request body and headers are never read or forwardedGET,HEAD,OPTIONS) fall straight through tonext(err)src/app.ts— mounts the middleware immediately afterexpress.json()so parse errors are caught before any route handler or the global error handlerWhy
Currently a malformed JSON body silently bubbles up to the global error handler and is logged with minimal context. Capturing parse failures at the source gives ops a structured, correlation-ID-linked log entry and returns a deterministic 400/413 to the client rather than a generic 500.
How to test
pnpm test src/middlewares/body-parse-error.middleware.test.ts14 tests cover:
success: falseentity.too.largeon a mutation method → 413next(err)called, no logErroron a mutation method → passes through unchangedApiError(withstatusCode) on a mutation method → passes through