fix: normalize NaN status to 500 in createError#159
Open
greymoth-jp wants to merge 1 commit into
Open
Conversation
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.
Behavior
createError(NaN)returns an error whosestatusandstatusCodeareNaN, with an empty message:Every other invalid status is normalized to 500:
The normalization step is supposed to catch this, but
NaN < 400andNaN >= 600are bothfalse, soNaNis the one number that slips past:This is reachable whenever a status is derived from input, e.g.
createError(Number(value))wherevalueis not numeric. The README documentsstatusCodeas defaulting to 500, so an invalid status likeNaNshould fall back to 500 the same way700does, instead of producing an error withstatus: NaN.Fix
Add an
isNaN(status)check to the existing normalization guard soNaNis handled like any other invalid status.isNaNkeeps this ES5-friendly, since the package still targetsnode >= 0.8. It only runs after thetypeof status !== 'number'check short-circuits, so no coercion happens for non-number values.Tests
Added a "when status is NaN" group asserting
createError(NaN)yields the 500InternalServerError(message, name, status, statusCode), mirroring the existing unknown 4xx/5xx tests. The full suite andnpm run lintpass.