Skip to content

Commit

Permalink
cast non-Errors to a string wrapped in a new Error
Browse files Browse the repository at this point in the history
  • Loading branch information
sebdeckers committed Nov 8, 2017
1 parent 947098d commit f18b396
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/handleRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ function preHandlerCallback (err, code) {
this.reply.send(payload)
}
}).catch((err) => {
this.reply.send(err)
if (err instanceof Error) {
this.reply.send(err)
} else {
this.reply.send(new Error(err || ''))
}
})
}
}
Expand Down
11 changes: 10 additions & 1 deletion test/reply-error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,10 @@ test('Error.status property support', t => {
})

test('Support rejection with values that are not Error instances', t => {
t.plan(1)
t.plan(2)
const fastify = Fastify()
const nonErr = {}
nonErr.toString = () => '💩'

fastify.get('/', () => {
return Promise.reject(nonErr)
Expand All @@ -384,5 +385,13 @@ test('Support rejection with values that are not Error instances', t => {
url: '/'
}, res => {
t.strictEqual(res.statusCode, 500)
t.deepEqual(
{
error: statusCodes['500'],
message: '💩',
statusCode: 500
},
JSON.parse(res.payload)
)
})
})

0 comments on commit f18b396

Please sign in to comment.