@hapipal/avocat
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/hapipal__avocat package

3.1.0 • Public • Published

avocat

A utility to convert Objection database errors into Boom HTTP errors

Build Status Coverage Status

Lead Maintainer: Daniel Cole

Installation

npm install @hapipal/avocat

Usage

See also the API Reference

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]) which transforms database errors from Objection into Boom HTTP errors that are compatible with hapi.

'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.

Package Sidebar

Install

npm i @hapipal/avocat

Weekly Downloads

515

Version

3.1.0

License

MIT

Unpacked Size

4.99 kB

Total Files

5

Last publish

Collaborators

  • devinivy
  • optii
  • mattboutet