Skip to content

Commit

Permalink
fix: correctly catch errors bubbling up to withConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
leemhenson committed Jun 14, 2018
1 parent eab4998 commit 21f7ae4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ export const catchAsTransactionRollbackError = (e: mixed) => new PgTransactionRo
export const isPgTransactionRollbackError = (e: Error): e is PgTransactionRollbackError =>
e instanceof PgTransactionRollbackError;

export class PgUnhandledConnectionUsageError extends Error {
constructor(public readonly error: mixed) {
super("An unhandled error occurred while a connection was in use.");

Error.captureStackTrace(this, PgUnhandledConnectionUsageError);
this.name = this.constructor.name;
}
}

export const catchAsUnhandledConnectionUsageError = (e: mixed) =>
new PgUnhandledConnectionUsageError(e);
export const ensureAsUnhandledConnectionUsageError = ensureError(
catchAsUnhandledConnectionUsageError,
);

export class PgUnhandledTransactionError extends Error {
constructor(public readonly error: mixed) {
super("An unhandled error occurred during a transaction.");
Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { constant } from "fp-ts/lib/function";
import { tryCatch } from "fp-ts/lib/TaskEither";
import { TaskEither, tryCatch } from "fp-ts/lib/TaskEither";
import * as pg from "pg";
import { catchAsTypeParserSetupError } from "./errors";
import { catchAsTypeParserSetupError, PgTypeParserSetupError } from "./errors";
import { parseInterval } from "./pgTypes/interval";
import { TypeParser, TypeParsers } from "./types";
import { SQL } from "./utils/sql";
Expand Down
9 changes: 8 additions & 1 deletion src/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
fromIOEither,
TaskEither,
taskEither,
tryCatch,
tryCatch as taskEitherTryCatch,
} from "fp-ts/lib/TaskEither";
import { Pool } from "pg";
Expand All @@ -20,10 +21,13 @@ import {
catchAsPoolCheckoutError,
catchAsPoolCreationError,
catchAsPoolShutdownError,
ensureAsUnhandledConnectionUsageError,
ensureAsUnhandledTransactionError,
isPgTransactionRollbackError,
} from "./errors";
import { setupParsers } from "./parser";
import { ConnectionE, ConnectionPool, ConnectionPoolConfig } from "./types";
import { eitherToPromise } from "./utils/eitherToPromise";

export const makeConnectionPool = (
poolConfig: ConnectionPoolConfig,
Expand Down Expand Up @@ -59,7 +63,10 @@ export const wrapConnectionPool = (pool: pg.Pool): ConnectionPool => ({
readerTryCatch<E, Error, pg.PoolClient>(() => pool.connect(), catchAsPoolCheckoutError)
.map(wrapPoolClient)
.map(connection =>
withConnection.value({ connection, environment }).fold<Either<Error, A>>(
tryCatch(
() => withConnection.run({ connection, environment }).then(eitherToPromise),
ensureAsUnhandledConnectionUsageError,
).fold<Either<Error, A>>(
err => {
connection.release(isPgTransactionRollbackError(err) ? err : undefined);
return left(err);
Expand Down

0 comments on commit 21f7ae4

Please sign in to comment.