Skip to content

Latest commit

 

History

History
59 lines (46 loc) · 1.72 KB

API.md

File metadata and controls

59 lines (46 loc) · 1.72 KB

API

A utility to convert Objection database errors into Boom HTTP errors

Note

Avocat is intended for use with hapi v19+, nodejs v12+, and Objection v2 or v3 (see v1 for lower support).

Avocat

Avocat.rethrow(error, [options])

Throws a Boom error according to the Objection error received.

  • error - the Objection error (any other types of errors are just ignored).
  • options - optional object where:
    • return - if true will return the error instead of throwing it.
    • includeMessage - if true the Boom error created will preserve the Objection error message.

Error mappings

These are the error mappings maintained by Avocat:

  • NotFoundError → 404 Not Found
  • ValidationError → 400 Bad Request
  • NotNullViolationError → 400 Bad Request
  • ConstraintViolationError → 400 Bad Request
  • ForeignKeyViolationError → 400 Bad Request
  • DataError → 400 Bad Request
  • CheckViolationError → 400 Bad Request
  • UniqueViolationError → 409 Conflict
  • DBError → 500 Internal Server Error

Example

'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;
        }
    }
});