Skip to content

Commit

Permalink
feat: rename QueryCancelledError to StatementCancelledError
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Renamed QueryCancelledError to StatementCancelledError.
  • Loading branch information
gajus committed Nov 9, 2019
1 parent 3f5527a commit 76cf1f7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
8 changes: 4 additions & 4 deletions .README/ERROR_HANDLING.md
Expand Up @@ -122,11 +122,11 @@ if (row) {

`NotNullIntegrityConstraintViolationError` is thrown when PostgreSQL responds with [`not_null_violation`](https://www.postgresql.org/docs/9.4/static/errcodes-appendix.html) (`23502`) error.

### Handling `QueryCancelledError`
### Handling `StatementCancelledError`

`QueryCancelledError` is thrown when a query is cancelled by the user, i.e. [`pg_cancel_backend`](https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL).
`StatementCancelledError` is thrown when a query is cancelled by the user (i.e. [`pg_cancel_backend`](https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL)) or in case of a timeout.

It should be safe to use the same connection if the `QueryCancelledError` is handled, e.g.
It should be safe to use the same connection if `StatementCancelledError` is handled, e.g.

```js
await pool.connect(async (connection0) => {
Expand All @@ -140,7 +140,7 @@ await pool.connect(async (connection0) => {
try {
await connection1.query(sql`SELECT pg_sleep(30)`);
} catch (error) {
if (error instanceof QueryCancelledError) {
if (error instanceof StatementCancelledError) {
// Safe to continue using the same connection.
} else {
throw error;
Expand Down
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -113,7 +113,7 @@ Note: Using this project does not require TypeScript or Flow. It is a regular ES
* [Handling `ForeignKeyIntegrityConstraintViolationError`](#slonik-error-handling-handling-foreignkeyintegrityconstraintviolationerror)
* [Handling `NotFoundError`](#slonik-error-handling-handling-notfounderror)
* [Handling `NotNullIntegrityConstraintViolationError`](#slonik-error-handling-handling-notnullintegrityconstraintviolationerror)
* [Handling `QueryCancelledError`](#slonik-error-handling-handling-querycancellederror)
* [Handling `StatementCancelledError`](#slonik-error-handling-handling-statementcancellederror)
* [Handling `UniqueIntegrityConstraintViolationError`](#slonik-error-handling-handling-uniqueintegrityconstraintviolationerror)
* [Types](#slonik-types)
* [Debugging](#slonik-debugging)
Expand Down Expand Up @@ -1880,12 +1880,12 @@ if (row) {
`NotNullIntegrityConstraintViolationError` is thrown when PostgreSQL responds with [`not_null_violation`](https://www.postgresql.org/docs/9.4/static/errcodes-appendix.html) (`23502`) error.
<a name="slonik-error-handling-handling-querycancellederror"></a>
### Handling <code>QueryCancelledError</code>
<a name="slonik-error-handling-handling-statementcancellederror"></a>
### Handling <code>StatementCancelledError</code>
`QueryCancelledError` is thrown when a query is cancelled by the user, i.e. [`pg_cancel_backend`](https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL).
`StatementCancelledError` is thrown when a query is cancelled by the user (i.e. [`pg_cancel_backend`](https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL)) or in case of a timeout.
It should be safe to use the same connection if the `QueryCancelledError` is handled, e.g.
It should be safe to use the same connection if `StatementCancelledError` is handled, e.g.
```js
await pool.connect(async (connection0) => {
Expand All @@ -1899,7 +1899,7 @@ await pool.connect(async (connection0) => {
try {
await connection1.query(sql`SELECT pg_sleep(30)`);
} catch (error) {
if (error instanceof QueryCancelledError) {
if (error instanceof StatementCancelledError) {
// Safe to continue using the same connection.
} else {
throw error;
Expand Down
4 changes: 2 additions & 2 deletions src/errors.js
Expand Up @@ -12,12 +12,12 @@ export class UnexpectedStateError extends SlonikError {}

export class ConnectionError extends SlonikError {}

export class QueryCancelledError extends SlonikError {
export class StatementCancelledError extends SlonikError {
constructor (error: Error) {
super();

this.originalError = error;
this.message = 'Query has been cancelled.';
this.message = 'Statement has been cancelled.';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -47,8 +47,8 @@ export {
InvalidInputError,
NotFoundError,
NotNullIntegrityConstraintViolationError,
QueryCancelledError,
SlonikError,
StatementCancelledError,
UnexpectedStateError,
UniqueIntegrityConstraintViolationError,
} from './errors';
5 changes: 2 additions & 3 deletions src/routines/executeQuery.js
Expand Up @@ -19,7 +19,7 @@ import {
ForeignKeyIntegrityConstraintViolationError,
InvalidInputError,
NotNullIntegrityConstraintViolationError,
QueryCancelledError,
StatementCancelledError,
UnexpectedStateError,
UniqueIntegrityConstraintViolationError,
} from '../errors';
Expand Down Expand Up @@ -164,8 +164,7 @@ export default async (
}

if (error.code === '57014') {
// @todo Rename `QueryCancelledError` to `BackendCancelledError`.
throw new QueryCancelledError(error);
throw new StatementCancelledError(error);
}

if (error.code === '23502') {
Expand Down
16 changes: 9 additions & 7 deletions test/slonik/integration.js
Expand Up @@ -4,10 +4,11 @@ import test, {
beforeEach,
} from 'ava';
import {
QueryCancelledError,
BackendTerminatedError,
createPool,
sql,
StatementCancelledError,
StatementTimeoutError,
} from '../../src';

const TEST_DSN = 'postgres://localhost/slonik_test';
Expand Down Expand Up @@ -171,7 +172,7 @@ test('returns expected query result object (DELETE)', async (t) => {
});
});

test('cancelled backend produces QueryCancelledError error', async (t) => {
test('terminated backend produces BackendTerminatedError error', async (t) => {
const pool = createPool(TEST_DSN);

const error = await t.throwsAsync(pool.connect(async (connection) => {
Expand All @@ -180,16 +181,16 @@ test('cancelled backend produces QueryCancelledError error', async (t) => {
`);

setTimeout(() => {
pool.query(sql`SELECT pg_cancel_backend(${connectionPid})`);
pool.query(sql`SELECT pg_terminate_backend(${connectionPid})`);
}, 100);

await connection.query(sql`SELECT pg_sleep(2)`);
}));

t.true(error instanceof QueryCancelledError);
t.true(error instanceof BackendTerminatedError);
});

test('terminated backend produces BackendTerminatedError error', async (t) => {
test('cancelled statement produces StatementCancelledError error', async (t) => {
const pool = createPool(TEST_DSN);

const error = await t.throwsAsync(pool.connect(async (connection) => {
Expand All @@ -198,11 +199,12 @@ test('terminated backend produces BackendTerminatedError error', async (t) => {
`);

setTimeout(() => {
pool.query(sql`SELECT pg_terminate_backend(${connectionPid})`);
pool.query(sql`SELECT pg_cancel_backend(${connectionPid})`);
}, 100);

await connection.query(sql`SELECT pg_sleep(2)`);
}));

t.true(error instanceof BackendTerminatedError);
t.true(error instanceof StatementCancelledError);
});
});

0 comments on commit 76cf1f7

Please sign in to comment.