Skip to content

Add errorHandler option #5

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

Closed
wants to merge 6 commits into from
Closed

Conversation

sjoerdsmink
Copy link
Contributor

@sjoerdsmink sjoerdsmink commented Dec 8, 2021

It might happen that:

  • The user starts the server
  • PostgreSQL crashes or reboots
  • In that case the connection pool is lost.

There is code to reconnect, but that's not executed as an uncaughtException is thrown. This is because this.emit("error", err); will cause the Node process to exit, as explained on https://nodejs.org/api/events.html#error-events

It would be better to offer the option to handle these errors.

Alternative would be to mention in the readme that errors can be handled when creating the adapter:

io.adapter(function (nsp) {
  const adapter = new PostgresAdapter(nsp, pool, {})
  adapter.on("error", (e) => {
    console.error("Postgres adapter error", e)
  })
  return adapter
})

The arrow notation (so io.adapter((nsp) => { instead of io.adapter(function (nsp) {) won't work in this case.

Add errorHandler option for adapter creation
@darrachequesne
Copy link
Member

Actually, I think we should rather remove the "error" events, as the end user has no constructive way to handle them.

See #7. What do you think?

@sjoerdsmink
Copy link
Contributor Author

I rewrote the code a bit to use an errorHandler inside the adapter. This way the type definitions of PostgresAdapterOptions are also correct. A user could directly create PostgresAdapter using the ops of PostgresAdapterOptions, in which case errorHandler should be implemented.

I think the benefit of using an errorHandler instead of removing all error events in #7 is that I can now log the errors. That is really useful for debugging, but also when going through server logging. I can understand your reasoning of not letting the server crash, but how would you feel about using the defaultErrorHandler to debug the message? So the user can still override this behaviour. So: const defaultErrorHandler = (err: any) => debug(err);

darrachequesne pushed a commit that referenced this pull request Dec 16, 2021
Before:

```js
io.adapter(createAdapter(pool));

io.of("/").adapter.on("error", () => {
  // needed so that no exception is thrown in case of error
});
```

After:

```js
io.adapter(createAdapter(pool, {
  errorHandler: (err) => {
    // ...
  }
}));
```
@darrachequesne
Copy link
Member

Merged as ec1b78c. 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.

2 participants