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

Check if headers were sent in controller #249

Closed
patrickhousley opened this issue Aug 12, 2017 · 3 comments
Closed

Check if headers were sent in controller #249

patrickhousley opened this issue Aug 12, 2017 · 3 comments

Comments

@patrickhousley
Copy link
Contributor

When attempting to use the response object in my controller to set up a succeed fast scenario, routing-controllers are still trying to write headers and send data when my controller finishes running. Example:

@JsonController('/auth')
export class ForgotPasswordController implements ForgotPasswordControllerAttributes {

  @Post('/forgotPassword')
  public async forgotPassword(
    @Res() response: express.Response,
    @Body({ validate: { skipMissingProperties: true } })
    body: ForgotPasswordRequest
  ) {
    // Response fast
    response.status(204).send();

    // Additional processing, might even use a return statement to end execution fast
  }
}

This results in error Unhandled Promise rejection: Error: Can't set headers after they are sent..

@MichalLytek
Copy link
Contributor

It happens because returning undefined means 404 Not Found for routing-controllers. So it tries to send error response. But it should wait with it until you return from an action - I think your handler is async and callback based, so try return promise instead. I cannot reproduce with simple fast sync return:

@Get("/raw/:id")
rawExpress(@Req() req: Request, @Res() res: Response) {
    res.json({ id: req.params.id })
}

Does the error Unhandled Promise rejection: Error: Can't set headers after they are sent. comes from your controller action or from routing-controllers code after your response 204? What is the error code of response in Postman?

@patrickhousley
Copy link
Contributor Author

Well how embarrassing. It would seem I had an error in my code that was throwing an exception. After tracking that down, it is working. I would say that solves this bug.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants