https://github.com/hapipal/avocat
A simple library to convert objection errors to boom errors
https://github.com/hapipal/avocat
Last synced: 12 months ago
JSON representation
A simple library to convert objection errors to boom errors
- Host: GitHub
- URL: https://github.com/hapipal/avocat
- Owner: hapipal
- License: mit
- Created: 2018-03-30T10:02:16.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-04-05T18:00:20.000Z (about 3 years ago)
- Last Synced: 2025-05-01T04:36:01.885Z (12 months ago)
- Language: JavaScript
- Size: 29.3 KB
- Stars: 8
- Watchers: 6
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# avocat
A utility to convert [Objection](https://vincit.github.io/objection.js/) database errors into [Boom](https://hapi.dev/module/boom/) HTTP errors
[](https://travis-ci.com/hapipal/avocat) [](https://coveralls.io/github/hapipal/avocat?branch=master)
Lead Maintainer: [Daniel Cole](https://github.com/optii)
## Installation
```sh
npm install @hapipal/avocat
```
## Usage
> See also the [API Reference](API.md)
>
> Avocat is intended for use with hapi v19+, nodejs v12+, and Objection v2 or v3 (_see v1 for lower support_).
Avocat provides a single utility function [`Avocat.rethrow(error, [options])`](API.md#avocatrethrowerror-options) which transforms database errors from Objection into Boom HTTP errors that are compatible with hapi.
```js
'use strict';
const Hapi = require('@hapi/hapi');
const Avocat = require('@hapipal/avocat');
const User = require('./user-model'); // An Objection model bound to a knex instance
const server = Hapi.server();
server.route({
method: 'get',
path: '/users/{id}',
handler(request) {
try {
return await User.query()
.findById(request.params.id)
.throwIfNotFound();
}
catch (err) {
Avocat.rethrow(err); // Throws a 404 Not Found if user does not exist
throw err;
}
}
});
```
## Extras
The interface for Avocat is heavily inspired by [Bounce](https://hapi.dev/module/bounce/).