Skip to content

Commit ec1b78c

Browse files
sjoerdsminkdarrachequesne
authored andcommitted
feat: add errorHandler option (#5)
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) => { // ... } })); ```
1 parent d6d74d5 commit ec1b78c

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ pool.query(`
5959
);
6060
`);
6161

62+
pool.on("error", (err) => {
63+
console.error("Postgres error", err);
64+
});
65+
6266
io.adapter(createAdapter(pool));
6367
io.listen(3000);
6468
```

lib/index.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export interface PostgresAdapterOptions {
8181
channelPrefix: string;
8282
/**
8383
* The name of the table for payloads over the 8000 bytes limit or containing binary data
84+
* @default "socket_io_attachments"
8485
*/
8586
tableName: string;
8687
/**
@@ -108,6 +109,12 @@ export interface PostgresAdapterOptions {
108109
* @default 30000
109110
*/
110111
cleanupInterval: number;
112+
/**
113+
* Handler for errors. If undefined, the errors will be simply logged.
114+
*
115+
* @default undefined
116+
*/
117+
errorHandler: (err: Error) => void;
111118
}
112119

113120
/**
@@ -136,6 +143,7 @@ export class PostgresAdapter extends Adapter {
136143
public heartbeatTimeout: number;
137144
public payloadThreshold: number;
138145
public cleanupInterval: number;
146+
public errorHandler: (err: Error) => void;
139147

140148
private readonly pool: Pool;
141149
private client: any;
@@ -169,6 +177,8 @@ export class PostgresAdapter extends Adapter {
169177
this.heartbeatTimeout = opts.heartbeatTimeout || 10000;
170178
this.payloadThreshold = opts.payloadThreshold || 8000;
171179
this.cleanupInterval = opts.cleanupInterval || 30000;
180+
const defaultErrorHandler = (err: Error) => debug(err);
181+
this.errorHandler = opts.errorHandler || defaultErrorHandler;
172182

173183
this.initSubscription();
174184
this.publish({
@@ -203,7 +213,7 @@ export class PostgresAdapter extends Adapter {
203213
try {
204214
await this.onEvent(msg.payload);
205215
} catch (err) {
206-
this.emit("error", err);
216+
this.errorHandler(err);
207217
}
208218
});
209219

@@ -218,7 +228,7 @@ export class PostgresAdapter extends Adapter {
218228

219229
this.client = client;
220230
} catch (err) {
221-
this.emit("error", err);
231+
this.errorHandler(err);
222232
debug("error while initializing client, scheduling reconnection...");
223233
this.scheduleReconnection();
224234
}
@@ -396,7 +406,7 @@ export class PostgresAdapter extends Adapter {
396406
`DELETE FROM ${this.tableName} WHERE created_at < now() - interval '${this.cleanupInterval} milliseconds'`
397407
);
398408
} catch (err) {
399-
this.emit("error", err);
409+
this.errorHandler(err);
400410
}
401411
this.scheduleCleanup();
402412
}, this.cleanupInterval);
@@ -434,7 +444,7 @@ export class PostgresAdapter extends Adapter {
434444

435445
this.scheduleHeartbeat();
436446
} catch (err) {
437-
this.emit("error", err);
447+
this.errorHandler(err);
438448
}
439449
}
440450

0 commit comments

Comments
 (0)