Skip to content

Commit

Permalink
feat: remove ability to customise errors
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Removing ability to configure NotFoundError.

Instead use a helper function to check if error is of certain type, e.g.

```js
// @flow

import {
  NotFoundError as SlonikNotFoundError
} from 'slonik';
import {
  NotFoundError as LocalNotFoundError
} from '../types';

export default (error: Error) => {
  return error instanceof SlonikNotFoundError || error instanceof LocalNotFoundError;
};

```
  • Loading branch information
gajus committed Oct 23, 2018
1 parent c00b21f commit 9c5b8b3
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 133 deletions.
22 changes: 0 additions & 22 deletions .README/ERROR_HANDLING.md
Expand Up @@ -49,28 +49,6 @@ pool.errors.NotFoundError === NotFoundError;

This is useful when the client configuration overrides the default error constructors.

### Overriding Error Constructor

Overriding the error constructor used by Slonik allows you to map database layer errors to your application errors.

```js
import {
createPool
} from 'slonik';

class NotFoundError extends Error {};

createPool('postgres://', {
errors: {
NotFoundError
}
});
```

The following error types can be overridden:

* `NotFoundError`

### Handling `NotFoundError`

To handle the case where query returns less than one row, catch `NotFoundError` error.
Expand Down
1 change: 0 additions & 1 deletion .README/USAGE.md
Expand Up @@ -47,7 +47,6 @@ type DatabaseConfigurationType =
|};

type ClientConfigurationType = {|
+errors?: ClientErrorsConfigurationType,
+interceptors?: $ReadOnlyArray<InterceptorType>
|};

Expand Down
35 changes: 2 additions & 33 deletions src/index.js
Expand Up @@ -297,9 +297,7 @@ export const one: InternalQueryOneFunctionType = async (connection, clientConfig
queryId
}, 'NotFoundError');

const ConfigurableNotFoundError = clientConfiguration.errors && clientConfiguration.errors.NotFoundError ? clientConfiguration.errors.NotFoundError : NotFoundError;

throw new ConfigurableNotFoundError();
throw new NotFoundError();
}

if (rows.length > 1) {
Expand Down Expand Up @@ -402,9 +400,7 @@ export const many: InternalQueryManyFunctionType = async (connection, clientConf
queryId
}, 'NotFoundError');

const ConfigurableNotFoundError = clientConfiguration.errors && clientConfiguration.errors.NotFoundError ? clientConfiguration.errors.NotFoundError : NotFoundError;

throw new ConfigurableNotFoundError();
throw new NotFoundError();
}

return rows;
Expand Down Expand Up @@ -578,15 +574,6 @@ export const createConnection = async (

return ended;
},
errors: {
CheckIntegrityConstraintViolationError,
DataIntegrityError,
ForeignKeyIntegrityConstraintViolationError,
NotFoundError: clientConfiguration.errors && clientConfiguration.errors.NotFoundError || NotFoundError,
NotNullIntegrityConstraintViolationError,
SlonikError,
UniqueIntegrityConstraintViolationError
},
many: mapTaggedTemplateLiteralInvocation(many.bind(null, connection, clientConfiguration)),
manyFirst: mapTaggedTemplateLiteralInvocation(manyFirst.bind(null, connection, clientConfiguration)),
maybeOne: mapTaggedTemplateLiteralInvocation(maybeOne.bind(null, connection, clientConfiguration)),
Expand Down Expand Up @@ -659,15 +646,6 @@ export const createPool = (
const bindConnection = {
any: mapTaggedTemplateLiteralInvocation(any.bind(null, connection, clientConfiguration)),
anyFirst: mapTaggedTemplateLiteralInvocation(anyFirst.bind(null, connection, clientConfiguration)),
errors: {
CheckIntegrityConstraintViolationError,
DataIntegrityError,
ForeignKeyIntegrityConstraintViolationError,
NotFoundError: clientConfiguration.errors && clientConfiguration.errors.NotFoundError || NotFoundError,
NotNullIntegrityConstraintViolationError,
SlonikError,
UniqueIntegrityConstraintViolationError
},
many: mapTaggedTemplateLiteralInvocation(many.bind(null, connection, clientConfiguration)),
manyFirst: mapTaggedTemplateLiteralInvocation(manyFirst.bind(null, connection, clientConfiguration)),
maybeOne: mapTaggedTemplateLiteralInvocation(maybeOne.bind(null, connection, clientConfiguration)),
Expand All @@ -688,15 +666,6 @@ export const createPool = (
any: mapTaggedTemplateLiteralInvocation(any.bind(null, pool, clientConfiguration)),
anyFirst: mapTaggedTemplateLiteralInvocation(anyFirst.bind(null, pool, clientConfiguration)),
connect,
errors: {
CheckIntegrityConstraintViolationError,
DataIntegrityError,
ForeignKeyIntegrityConstraintViolationError,
NotFoundError: clientConfiguration.errors && clientConfiguration.errors.NotFoundError || NotFoundError,
NotNullIntegrityConstraintViolationError,
SlonikError,
UniqueIntegrityConstraintViolationError
},
many: mapTaggedTemplateLiteralInvocation(many.bind(null, pool, clientConfiguration)),
manyFirst: mapTaggedTemplateLiteralInvocation(manyFirst.bind(null, pool, clientConfiguration)),
maybeOne: mapTaggedTemplateLiteralInvocation(maybeOne.bind(null, pool, clientConfiguration)),
Expand Down
14 changes: 0 additions & 14 deletions src/types.js
Expand Up @@ -24,12 +24,7 @@ type QueryResultType<T> = {
// eslint-disable-next-line flowtype/no-weak-types
type InternalDatabaseConnectionType = any;

export type ClientErrorsConfigurationType = {|
+NotFoundError?: Class<Error>
|};

export type ClientConfigurationType = {|
+errors?: ClientErrorsConfigurationType,
+interceptors?: $ReadOnlyArray<InterceptorType>
|};

Expand All @@ -48,15 +43,6 @@ export type DatabaseConfigurationType =
|};

export type DatabaseConnectionType = {
+errors: {|
+CheckIntegrityConstraintViolationError: Class<Error>,
+DataIntegrityError: Class<Error>,
+ForeignKeyIntegrityConstraintViolationError: Class<Error>,
+NotFoundError: Class<Error>,
+NotNullIntegrityConstraintViolationError: Class<Error>,
+SlonikError: Class<Error>,
+UniqueIntegrityConstraintViolationError: Class<Error>
|},
+any: QueryAnyFunctionType<*>,
+anyFirst: QueryAnyFirstFunctionType<*>,
+many: QueryManyFunctionType<*>,
Expand Down
21 changes: 0 additions & 21 deletions test/slonik/many.js
Expand Up @@ -4,14 +4,11 @@

import test from 'ava';
import sinon from 'sinon';
import ExtendableError from 'es6-error';
import {
many,
NotFoundError
} from '../../src';

class TestError extends ExtendableError {}

test('returns the query results rows', async (t) => {
const stub = sinon.stub().returns({
rows: [
Expand Down Expand Up @@ -51,21 +48,3 @@ test('throws an error if no rows are returned', async (t) => {

await t.throwsAsync(many(connection, {}, ''), NotFoundError);
});

test('throws an error if no rows are returned (user defined error constructor)', async (t) => {
const stub = sinon.stub().returns({
rows: []
});

const connection: any = {
query: stub
};

const clientConfiguration = {
errors: {
NotFoundError: TestError
}
};

await t.throwsAsync(many(connection, clientConfiguration, ''), TestError);
});
21 changes: 0 additions & 21 deletions test/slonik/one.js
Expand Up @@ -4,15 +4,12 @@

import test from 'ava';
import sinon from 'sinon';
import ExtendableError from 'es6-error';
import {
one,
DataIntegrityError,
NotFoundError
} from '../../src';

class TestError extends ExtendableError {}

test('returns the first row', async (t) => {
const stub = sinon.stub().returns({
rows: [
Expand Down Expand Up @@ -45,24 +42,6 @@ test('throws an error if no rows are returned', async (t) => {
await t.throwsAsync(one(connection, {}, ''), NotFoundError);
});

test('throws an error if no rows are returned (user defined error constructor)', async (t) => {
const stub = sinon.stub().returns({
rows: []
});

const connection: any = {
query: stub
};

const clientConfiguration = {
errors: {
NotFoundError: TestError
}
};

await t.throwsAsync(one(connection, clientConfiguration, ''), TestError);
});

test('throws an error if more than one row is returned', async (t) => {
const stub = sinon.stub().returns({
rows: [
Expand Down
21 changes: 0 additions & 21 deletions test/slonik/oneFirst.js
Expand Up @@ -4,15 +4,12 @@

import test from 'ava';
import sinon from 'sinon';
import ExtendableError from 'es6-error';
import {
oneFirst,
DataIntegrityError,
NotFoundError
} from '../../src';

class TestError extends ExtendableError {}

test('returns value of the first column from the first row', async (t) => {
const stub = sinon.stub().returns({
rows: [
Expand Down Expand Up @@ -43,24 +40,6 @@ test('throws an error if no rows are returned', async (t) => {
await t.throwsAsync(oneFirst(connection, {}, ''), NotFoundError);
});

test('throws an error if no rows are returned (user defined error constructor)', async (t) => {
const stub = sinon.stub().returns({
rows: []
});

const connection: any = {
query: stub
};

const clientConfiguration = {
errors: {
NotFoundError: TestError
}
};

await t.throwsAsync(oneFirst(connection, clientConfiguration, ''), TestError);
});

test('throws an error if more than one row is returned', async (t) => {
const stub = sinon.stub().returns({
rows: [
Expand Down

0 comments on commit 9c5b8b3

Please sign in to comment.