From 3ea3b38ba7ac265252c91becae8009098bcc0df9 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 13 Dec 2021 08:44:25 +0100 Subject: [PATCH] fix: do not emit "error" events Forcing users to handle "error" events is useless since there is no interesting way to handle them (no retry, for example). Before: ```js io.adapter(createAdapter(pool)); poll.on("error", (err) => { // client disconnection }); io.of("/").adapter.on("error", () => { // needed so that no exception is thrown }); ``` After: ```js io.adapter(createAdapter(pool)); poll.on("error", (err) => { // client disconnection }) ``` --- README.md | 5 +++++ lib/index.ts | 11 +++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b900045..9d0e4d1 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,11 @@ pool.query(` `); io.adapter(createAdapter(pool)); + +pool.on("error", (err) => { + // ... +}); + io.listen(3000); ``` diff --git a/lib/index.ts b/lib/index.ts index 1b9cd5e..1d96639 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -198,7 +198,7 @@ export class PostgresAdapter extends Adapter { try { await this.onEvent(msg.payload); } catch (err) { - this.emit("error", err); + debug("an error has occurred while handling the notification"); } }); @@ -213,7 +213,6 @@ export class PostgresAdapter extends Adapter { this.client = client; } catch (err) { - this.emit("error", err); debug("error while initializing client, scheduling reconnection..."); this.scheduleReconnection(); } @@ -391,7 +390,7 @@ export class PostgresAdapter extends Adapter { `DELETE FROM ${this.tableName} WHERE created_at < now() - interval '${this.cleanupInterval} milliseconds'` ); } catch (err) { - this.emit("error", err); + debug("an error has occurred while cleaning up old payloads"); } this.scheduleCleanup(); }, this.cleanupInterval); @@ -426,11 +425,11 @@ export class PostgresAdapter extends Adapter { this.channel, payload, ]); - - this.scheduleHeartbeat(); } catch (err) { - this.emit("error", err); + debug("an error has occurred while publishing a new notification"); } + + this.scheduleHeartbeat(); } private async publishWithAttachment(document: any) {