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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change reply in onSend hook using Fastify sensible #156

Open
2 tasks done
melroy89 opened this issue Mar 19, 2024 · 0 comments
Open
2 tasks done

Change reply in onSend hook using Fastify sensible #156

melroy89 opened this issue Mar 19, 2024 · 0 comments

Comments

@melroy89
Copy link

melroy89 commented Mar 19, 2024

Prerequisites

  • I have written a descriptive issue title
  • 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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant