0.4.0 • Published 8 years ago

hata v0.4.0

Weekly downloads
587
License
LGPL-3.0
Repository
github
Last release
8 years ago

Hata (HTTP Errors)

Travis CI npm npm Codecov VersionEye

Common HTTP errors to be used with express.js or whatever engine you use.

The name

Hata means "error" in Arabic (and Turkish!), which is where the package name comes from.

Features

The following error types are currently supported:

  • (400) bad-request: BadRequestError
  • (401) unauthorized: UnauthorizedError
  • (403) forbidden: ForbiddenError
  • (404) not-found: NotFoundError
  • (409) conflict: ConflictError
  • (422) unprocessable-entity: UnprocessableEntityError

The error object is an instance of Error, with the properties name, message and stack.

Usage

var NotFoundError = require('hata/not-found');

throw new NotFoundError('Product not found');

You can also add more properties to the error object by passing them as the second parameter.

var NotFoundError = require('hata/not-found');

var error = new NotFoundError('Product not found', { item: 'product', id: 1, });
// error now has attributes `item` and `id`.

You can also use it in the following way:

var hata = require('hata');
var error = hata(404/*, message, obj*/);

Express

Just add a regular error handler like the following:

// http://expressjs.com/guide/error-handling.html
app.use(function(err, req, res, next) { // jshint ignore:line
  return res
            .status(err.httpCode || 500)
            .header('content-type: application/json')
            .send(safeJsonStringify(err));
});
0.4.0

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.0

9 years ago

0.1.0

9 years ago