Skip to content

Commit

Permalink
feat: expose instance specific error constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Oct 23, 2018
1 parent e15bd72 commit c00b21f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .README/ERROR_HANDLING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,38 @@ try {

```

### Instance specific errors

You can import instance specific errors from the connection object, e.g.

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

class NotFoundError extends Error {};

const pool = createPool('postgres://', {
errors: {
NotFoundError
}
});

// +errors: {|
// +CheckIntegrityConstraintViolationError: Class<Error>,
// +DataIntegrityError: Class<Error>,
// +ForeignKeyIntegrityConstraintViolationError: Class<Error>,
// +NotFoundError: Class<Error>,
// +NotNullIntegrityConstraintViolationError: Class<Error>,
// +SlonikError: Class<Error>,
// +UniqueIntegrityConstraintViolationError: Class<Error>
// |},
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.
Expand Down
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,15 @@ 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 @@ -650,6 +659,15 @@ 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 @@ -670,6 +688,15 @@ 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
9 changes: 9 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ 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

0 comments on commit c00b21f

Please sign in to comment.