Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes crash when using a non-standard error code #2184

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,12 @@ Reply.prototype.headers = function (headers) {
}

Reply.prototype.code = function (code) {
if (statusCodes[code] === undefined) {
const intValue = parseInt(code)
if (isNaN(intValue) || intValue < 100 || intValue > 600) {
throw new FST_ERR_BAD_STATUS_CODE()
leorossi marked this conversation as resolved.
Show resolved Hide resolved
}

this.res.statusCode = code
this.res.statusCode = intValue
this[kReplyHasStatusCode] = true
return this
}
Expand Down
15 changes: 15 additions & 0 deletions test/reply-error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,18 @@ invalidErrorCodes.forEach((invalidCode) => {
})
})
})

test('should not throw error for status code in range', t => {
t.plan(2)
const fastify = Fastify()
fastify.get('/', (request, reply) => {
return reply.code(525).send('Hello World')
})
fastify.inject({
url: '/',
method: 'GET'
}, (e, res) => {
t.is(525, res.statusCode)
t.is('Hello World', res.body)
})
})