fastify-sensible icon indicating copy to clipboard operation
fastify-sensible copied to clipboard

Change reply in onSend hook using Fastify sensible

Open melroy89 opened this issue 11 months ago • 0 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

I would love to use fastify-sensible. But in my case I check if the payload is empty and I want to set the "Not found" generic error using the reply.notFound() function.

However, it seems that it automatically also triggers a send again or something..?

Anyway, the function call will cause errors in onSend too bad 😢 (I do see the "not found" JSON message btw when calling the API, you only get those internal errors):

[14:29:07 UTC] ERROR: Promise errored, but reply.sent = true was set
    reqId: "req-1"
    err: {
      "type": "Error",
      "message": "Cannot write headers after they are sent to the client",
      "stack":
          Error [ERR_HTTP_HEADERS_SENT]: Cannot write headers after they are sent to the client
              at ServerResponse.writeHead (node:_http_server:345:11)
              at /media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/error-handler.js:42:19
              at fallbackErrorHandler (/media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/error-handler.js:127:3)
              at handleError (/media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/error-handler.js:34:5)
              at onErrorHook (/media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/reply.js:839:5)
              at wrapOnSendEnd (/media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/reply.js:563:5)
              at next (/media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/hooks.js:297:7)
              at onSendHookRunner (/media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/hooks.js:317:3)
              at onSendHook (/media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/reply.js:549:5)
              at Reply.send (/media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/reply.js:162:5)
      "code": "ERR_HTTP_HEADERS_SENT"
    }

Motivation

Currently I was creating my own errors like so, but I would like to leverage Fastify sensible in the onSend instead of reinventing the wheel here:

      reply.code(404).header('content-type', 'application/json; charset=utf-8')
      // Not found message
      const stringify = fastJson({
        type: 'object',
        properties: {
          message: {
            type: 'string'
          }
        }
      })
      const message = { message: 'Not found' }
      return done(null, stringify(message))

Example

Code that cause the error:

fastify.addHook('onSend', (req, reply, payload, done) => {
    if (!payload) {
      reply.notFound()
      return done()
    }
    
    ..... 
    .....
    done()
}

melroy89 avatar Mar 19 '24 14:03 melroy89