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

Extend support for Express middlewares to include error handling #674

Merged
merged 3 commits into from
May 1, 2023

Conversation

cieldeville
Copy link
Contributor

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behaviour

Currently, the next function passed to express-style middlewares does not take into account any potential errors handed to it. If a express-style middleware fails, the connection and all following middlewares will still be executed and a connection will be established successfully.

New behaviour

If any value other than undefined is passed to the next function, all further middlewares will not be executed. The socket will be closed, receiving an error "Bad Request" as if the verify function in server.ts had failed. This behaviour is more consistent with how express handles errors emerging in its middleware (see here for details).

Other information (e.g. related issues)

I mostly wrote the code for this pull request to see if there was any obvious obstacles with handling errors as described above. I had recently found myself in a situation where I wanted to verify CSRF tokens during connection establishment but was unable to fail connection attempts from within middlewares.

Obviously, there may be things I have missed or possibly even good reasons to not allow any such error handling at all. If there are, please dismiss this pull request. Additionally, it may be more advantageous to return a custom, new type of error or possibly to not return a 'middleware failure' token in the error context out of security considerations.

I have added two tests for failing middlewares (both polling and websockets).

Thank you for your time!

…re functions. Should an error be encountered, it will be propagated upwards and no further middlewares will be executed.
Copy link

@haneenmahd haneenmahd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think think this is a good practice to abort connections if any errors have been occurRed. This allows in more flexibility and control of the socket for the developer. @darrachequesne What do you think?

@darrachequesne darrachequesne merged commit 9395782 into socketio:main May 1, 2023
@darrachequesne
Copy link
Member

@cieldeville awesome, thanks!

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

Successfully merging this pull request may close these issues.

3 participants