/knex-generic-errors

General errors capability for the awesome Knex (http://knexjs.org), multi RDBMS query builder.

Primary LanguageJavaScriptMIT LicenseMIT

knex-generic-errors Build Status

A module for knex.js that enables using more detailed "typed" errors instead of plain javascript Error.

Supported knex versions:

  • ^0.10
  • ^0.11
  • ^0.12

Supported dialects:

Installation

$ npm install knex-generic-errors --save-dev

Usage

As Knex actually doesn't have an API for plugins this module has to be used as seen below.

var Knex = require('knex');
var knexGenericErrors = require('knex-generic-errors');

// Wrap Knex initializer
var knex = knexGenericErrors.attach(Knex, function() {
  // Initialize Knex
  return Knex(/* connection configuration */);
});

// Use `knex`
knex.raw('SELECT * FROM')
.catch(Knex.Errors.QueryError, function(err){
	/* eg. log query error */
});

knexGenericErrors.Errors === Knex.Errors; // true
knex.Errors === Knex.Errors; // true

API

knex-generic-errors

Method/Property Arguments Returns Description
attach(knex, knexInstanceResolver)
knex
Knex initializer method
      <dt>knexInstanceResolver</dt>
      <dd>
        <span>A function that gets executed internally to receive initialized knex instance.</span>
        <dl>
          <dt>-</dt>
          <dd>-</dd>
        </dl>
      </dd>
    </dl>
  </td>
  <td>
  	<dl>
      <dt>knexInstance</dt>
      <dd>
        An initialized knex instance.
      </dd>
    </dl>
  </td>
  <td>Enables more detailed errors on Knex and knex instance</td>
</tr>
<tr>
  <td><a name="knex-generic-errors-Errors"></a><b>Errors</b></td>
  <td>-</td>
  <td>-</td>
  <td>Property that holds error types. Error types listed under <a href="#errors">Errors</a></td>
</tr>

Errors

Following errors can be accessed through knex-generic-errors property Errors or after attaching to instance from Knex.Errors or knex instance knex.Errors property.

Error Description
ConnectionError Is thrown if any connection error occurs between knex and database
QueryError Is thrown if invalid query was executed
DatabaseError Is thrown if database internal error occurs

Running Tests

Tests can be runned without any database in place.

$ npm install
$ npm test

Copyright and License

Copyright Kaarel Raspel, 2016

MIT Licence