@@ -81,6 +81,7 @@ export interface PostgresAdapterOptions {
81
81
channelPrefix : string ;
82
82
/**
83
83
* The name of the table for payloads over the 8000 bytes limit or containing binary data
84
+ * @default "socket_io_attachments"
84
85
*/
85
86
tableName : string ;
86
87
/**
@@ -108,6 +109,12 @@ export interface PostgresAdapterOptions {
108
109
* @default 30000
109
110
*/
110
111
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 ;
111
118
}
112
119
113
120
/**
@@ -136,6 +143,7 @@ export class PostgresAdapter extends Adapter {
136
143
public heartbeatTimeout : number ;
137
144
public payloadThreshold : number ;
138
145
public cleanupInterval : number ;
146
+ public errorHandler : ( err : Error ) => void ;
139
147
140
148
private readonly pool : Pool ;
141
149
private client : any ;
@@ -169,6 +177,8 @@ export class PostgresAdapter extends Adapter {
169
177
this . heartbeatTimeout = opts . heartbeatTimeout || 10000 ;
170
178
this . payloadThreshold = opts . payloadThreshold || 8000 ;
171
179
this . cleanupInterval = opts . cleanupInterval || 30000 ;
180
+ const defaultErrorHandler = ( err : Error ) => debug ( err ) ;
181
+ this . errorHandler = opts . errorHandler || defaultErrorHandler ;
172
182
173
183
this . initSubscription ( ) ;
174
184
this . publish ( {
@@ -203,7 +213,7 @@ export class PostgresAdapter extends Adapter {
203
213
try {
204
214
await this . onEvent ( msg . payload ) ;
205
215
} catch ( err ) {
206
- this . emit ( "error" , err ) ;
216
+ this . errorHandler ( err ) ;
207
217
}
208
218
} ) ;
209
219
@@ -218,7 +228,7 @@ export class PostgresAdapter extends Adapter {
218
228
219
229
this . client = client ;
220
230
} catch ( err ) {
221
- this . emit ( "error" , err ) ;
231
+ this . errorHandler ( err ) ;
222
232
debug ( "error while initializing client, scheduling reconnection..." ) ;
223
233
this . scheduleReconnection ( ) ;
224
234
}
@@ -396,7 +406,7 @@ export class PostgresAdapter extends Adapter {
396
406
`DELETE FROM ${ this . tableName } WHERE created_at < now() - interval '${ this . cleanupInterval } milliseconds'`
397
407
) ;
398
408
} catch ( err ) {
399
- this . emit ( "error" , err ) ;
409
+ this . errorHandler ( err ) ;
400
410
}
401
411
this . scheduleCleanup ( ) ;
402
412
} , this . cleanupInterval ) ;
@@ -434,7 +444,7 @@ export class PostgresAdapter extends Adapter {
434
444
435
445
this . scheduleHeartbeat ( ) ;
436
446
} catch ( err ) {
437
- this . emit ( "error" , err ) ;
447
+ this . errorHandler ( err ) ;
438
448
}
439
449
}
440
450
0 commit comments