Skip to content
This repository was archived by the owner on Mar 18, 2022. It is now read-only.

Commit a925be9

Browse files
Gindenpleerock
authored andcommitted
feat: add postgres pool error handler (typeorm#4474)
Add option to customize pool error handling. Users can decide to log these errors with higher level (eg. error), crash application or reconnect.
1 parent f65ecc7 commit a925be9

7 files changed

Lines changed: 36 additions & 3 deletions

File tree

docs/connection-options.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options).
180180

181181
* `uuidExtension` - The Postgres extension to use when generating UUIDs. Defaults to `uuid-ossp`. Can be changed to `pgcrypto` if the `uuid-ossp` extension is unavailable.
182182

183+
* `poolErrorHandler` - A function that get's called when underlying pool emits `'error'` event. Takes single parameter (error instance) and defaults to logging with `warn` level.
184+
183185
## `sqlite` connection options
184186

185187
* `database` - Database path. For example "./mydb.sql"
@@ -258,6 +260,8 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options).
258260
* `pool.idleTimeoutMillis` - the minimum amount of time that an object may sit idle in the pool before it is eligible for
259261
eviction due to idle time. Supersedes `softIdleTimeoutMillis`. Default: `30000`.
260262

263+
* `pool.errorHandler` - A function that get's called when underlying pool emits `'error'` event. Takes single parameter (error instance) and defaults to logging with `warn` level.
264+
261265
* `options.fallbackToDefaultDb` - By default, if the database requestion by `options.database` cannot be accessed, the connection
262266
will fail with an error. However, if `options.fallbackToDefaultDb` is set to `true`, then the user's default database will
263267
be used instead (Default: `false`).

src/driver/cockroachdb/CockroachConnectionOptions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ export interface CockroachConnectionOptions extends BaseConnectionOptions, Cockr
3333

3434
};
3535

36+
37+
/*
38+
* Function handling errors thrown by drivers pool.
39+
* Defaults to logging error with `warn` level.
40+
*/
41+
readonly poolErrorHandler?: (err: any) => any;
42+
3643
}

src/driver/cockroachdb/CockroachDriver.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,14 @@ export class CockroachDriver implements Driver {
710710
// create a connection pool
711711
const pool = new this.postgres.Pool(connectionOptions);
712712
const { logger } = this.connection;
713+
714+
const poolErrorHandler = options.poolErrorHandler || ((error: any) => logger.log("warn", `Postgres pool raised an error. ${error}`));
715+
713716
/*
714717
Attaching an error handler to pool errors is essential, as, otherwise, errors raised will go unhandled and
715718
cause the hosting app to crash.
716719
*/
717-
pool.on("error", (error: any) => logger.log("warn", `Postgres pool raised an error. ${error}`));
720+
pool.on("error", poolErrorHandler);
718721

719722
return new Promise((ok, fail) => {
720723
pool.connect((err: any, connection: any, release: Function) => {

src/driver/postgres/PostgresConnectionOptions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ export interface PostgresConnectionOptions extends BaseConnectionOptions, Postgr
3939
* If uuid-ossp is selected, TypeORM will use the uuid_generate_v4() function from this extension.
4040
*/
4141
readonly uuidExtension?: "pgcrypto" | "uuid-ossp";
42+
43+
44+
/*
45+
* Function handling errors thrown by drivers pool.
46+
* Defaults to logging error with `warn` level.
47+
*/
48+
readonly poolErrorHandler?: (err: any) => any;
4249
}

src/driver/postgres/PostgresDriver.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,11 +894,14 @@ export class PostgresDriver implements Driver {
894894
// create a connection pool
895895
const pool = new this.postgres.Pool(connectionOptions);
896896
const { logger } = this.connection;
897+
898+
const poolErrorHandler = options.poolErrorHandler || ((error: any) => logger.log("warn", `Postgres pool raised an error. ${error}`));
899+
897900
/*
898901
Attaching an error handler to pool errors is essential, as, otherwise, errors raised will go unhandled and
899902
cause the hosting app to crash.
900903
*/
901-
pool.on("error", (error: any) => logger.log("warn", `Postgres pool raised an error. ${error}`));
904+
pool.on("error", poolErrorHandler);
902905

903906
return new Promise((ok, fail) => {
904907
pool.connect((err: any, connection: any, release: Function) => {

src/driver/sqlserver/SqlServerConnectionOptions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ export interface SqlServerConnectionOptions extends BaseConnectionOptions, SqlSe
106106
* to idle time. Supercedes softIdleTimeoutMillis Default: 30000
107107
*/
108108
readonly idleTimeoutMillis?: number;
109+
110+
/*
111+
* Function handling errors thrown by drivers pool.
112+
* Defaults to logging error with `warn` level.
113+
*/
114+
readonly errorHandler?: (err: any) => any;
109115
};
110116

111117
/**
@@ -271,4 +277,5 @@ export interface SqlServerConnectionOptions extends BaseConnectionOptions, SqlSe
271277

272278
};
273279

280+
274281
}

src/driver/sqlserver/SqlServerDriver.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,11 +749,13 @@ export class SqlServerDriver implements Driver {
749749
const pool = new this.mssql.ConnectionPool(connectionOptions);
750750

751751
const { logger } = this.connection;
752+
753+
const poolErrorHandler = (options.pool && options.pool.errorHandler) || ((error: any) => logger.log("warn", `MSSQL pool raised an error. ${error}`));
752754
/*
753755
Attaching an error handler to pool errors is essential, as, otherwise, errors raised will go unhandled and
754756
cause the hosting app to crash.
755757
*/
756-
pool.on("error", (error: any) => logger.log("warn", `MSSQL pool raised an error. ${error}`));
758+
pool.on("error", poolErrorHandler);
757759

758760
const connection = pool.connect((err: any) => {
759761
if (err) return fail(err);

0 commit comments

Comments
 (0)