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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pool.query(`
);
`);

pool.on("error", (err) => {
console.error("Postgres error", err);
});

io.adapter(createAdapter(pool));
io.listen(3000);
```
Expand Down
17 changes: 13 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export interface PostgresAdapterOptions {
channelPrefix: string;
/**
* The name of the table for payloads over the 8000 bytes limit or containing binary data
* @default "socket_io_attachments"
*/
tableName: string;
/**
Expand Down Expand Up @@ -107,6 +108,11 @@ export interface PostgresAdapterOptions {
* @default 30000
*/
cleanupInterval: number;
/**
* Handler for errors. If undefined, it will terminate the process when an error occurs (see https://nodejs.org/api/events.html#error-events).
* @default undefined
*/
errorHandler: (err: any) => void | undefined
}

/**
Expand Down Expand Up @@ -135,6 +141,7 @@ export class PostgresAdapter extends Adapter {
public heartbeatTimeout: number;
public payloadThreshold: number;
public cleanupInterval: number;
public errorHandler: (err: any) => void;

private readonly pool: any;
private client: any;
Expand Down Expand Up @@ -164,6 +171,8 @@ export class PostgresAdapter extends Adapter {
this.heartbeatTimeout = opts.heartbeatTimeout || 10000;
this.payloadThreshold = opts.payloadThreshold || 8000;
this.cleanupInterval = opts.cleanupInterval || 30000;
const defaultErrorHandler = (err: any) => debug(err);
this.errorHandler = opts.errorHandler || defaultErrorHandler;

this.initSubscription();
this.publish({
Expand Down Expand Up @@ -198,7 +207,7 @@ export class PostgresAdapter extends Adapter {
try {
await this.onEvent(msg.payload);
} catch (err) {
this.emit("error", err);
this.errorHandler(err);
}
});

Expand All @@ -213,7 +222,7 @@ export class PostgresAdapter extends Adapter {

this.client = client;
} catch (err) {
this.emit("error", err);
this.errorHandler(err);
debug("error while initializing client, scheduling reconnection...");
this.scheduleReconnection();
}
Expand Down Expand Up @@ -391,7 +400,7 @@ export class PostgresAdapter extends Adapter {
`DELETE FROM ${this.tableName} WHERE created_at < now() - interval '${this.cleanupInterval} milliseconds'`
);
} catch (err) {
this.emit("error", err);
this.errorHandler(err);
}
this.scheduleCleanup();
}, this.cleanupInterval);
Expand Down Expand Up @@ -429,7 +438,7 @@ export class PostgresAdapter extends Adapter {

this.scheduleHeartbeat();
} catch (err) {
this.emit("error", err);
this.errorHandler(err);
}
}

Expand Down