Fastify: Custom error statusCode

Created on 30 Oct 2017  路  4Comments  路  Source: fastify/fastify

(Apologies for the simple question. I'm completely new to Fastify and could not yet find the answer in the docs/examples/issues/source.)

Question

Can a thrown Error instance set the outgoing HTTP status code?

Use Cases

Example

const fastify = require('fastify')({ logger: true })

fastify.get('/', async function (request, reply) {
  const error = new Error()
  error.message = 'This is the message'
  error.statusCode = 404
  error.error = 'This is the error'
  throw error
})

fastify.listen(3000, function (err) {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)
})

Actual Output

$ curl --verbose http://[::1]:3000/
*   Trying ::1...
* TCP_NODELAY set
* Connected to ::1 (::1) port 3000 (#0)
> GET / HTTP/1.1
> Host: [::1]:3000
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 500 Internal Server Error
< Content-Type: application/json
< Content-Length: 82
< Date: Mon, 30 Oct 2017 04:17:12 GMT
< Connection: keep-alive
< 
* Connection #0 to host ::1 left intact
{"error":"Internal Server Error","message":"This is the message","statusCode":500}

Expected Output

< HTTP/1.1 404 Internal Server Error
[...]
{"error":"Internal Server Error","message":"This is the message","statusCode":404}
question

Most helpful comment

I thought that was supported.
IMHO we should change this block (https://github.com/fastify/fastify/blob/master/lib/reply.js#L176-L203) so that it checks if an error has a statusCode聽 property, and use that instead of 500.
Would you mind opening up a PR?

All 4 comments

Hi! Thank you for checking out Fastify!
You can do that in three ways:

// With standard function
fastify.get('/', (req, reply) => {
   reply.code(404).send(new Error('Missing this'))
})

// With async  function
fastify.get('/', async (req, reply) => {
   reply.code(404)
   return new Error('Missing this')
})

// Using throw inside an async  function
fastify.get('/', async (req, reply) => {
   reply.code(404)
   throw new Error('Missing this')
})

I thought that was supported.
IMHO we should change this block (https://github.com/fastify/fastify/blob/master/lib/reply.js#L176-L203) so that it checks if an error has a statusCode聽 property, and use that instead of 500.
Would you mind opening up a PR?

@mcollina @sebdeckers This behavior surprised me!

I had a bug in my code. My code was using the AWS SDK and failed to catch one of the exceptions it could throw. I was confused because when that exception was thrown (and uncaught) my API response code was 400 -- I had expected an uncaught exception to trigger a 500 error. After some digging, I realized the uncaught AWS exception happened to have a field named "statusCode" and that fastify was using that.

I've since fixed the bug in my code which left this third-party exception unhandled. But in case I miss other third-party exceptions which unwittingly trigger this special behavior in fastify, I've updated my custom error handler to check for the statusCode field in the uncaught exception, and if present set the status code to 500 (i.e., uncaught exceptions should always be a 500 error in my codebase).

Perhaps this behavior should be documented on Errors page? Apologies if it is documented somewhere else and I overlooked it.

Would you like to send a PR? This behavior is needed is to support https://www.npmjs.com/package/http-errors as Express does.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

avilang picture avilang  路  3Comments

Jegannathan picture Jegannathan  路  3Comments

mcollina picture mcollina  路  3Comments

allevo picture allevo  路  3Comments

rabbitooops picture rabbitooops  路  3Comments