Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Sep 8, 2014
0 parents commit 4ce1368
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@

.DS_Store*
*.log
*.gz

node_modules
coverage
9 changes: 9 additions & 0 deletions .travis.yml
@@ -0,0 +1,9 @@
node_js:
- "0.6"
- "0.8"
- "0.10"
- "0.11"
language: node_js
script: "npm run-script test-travis"
before_install: "npm update -g npm"
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2014 Jonathan Ong me@jongleberry.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
@@ -0,0 +1,63 @@

# http-errors

[![NPM version][npm-image]][npm-url]
[![Build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Dependency Status][david-image]][david-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]

Create HTTP errors for Express, Koa, Connect, etc. with ease.

## Example

```js
var createError = require('http-errors');

app.use(function (req, res, next) {
if (!req.user) return next(createError(401, 'Please login to view this page.'));
next();
})
```

## API

This is the current API, currently extracted from Koa and subject to change.

### Error Properties

- `message`
- `status` and `statusCode` - the status code of the error, defaulting to `500`

### createError([status], [message], [properties])

```js
var err = createError(404, 'This video does not exist!');
```

- `status: 500` - the status code as a number
- `message` - the message of the error, defaulting to node's text for that status code.
- `properties` - custom properties to attach to the object

### new createError\[code || name\](\[msg]\))

```js
var err = new createError.NotFound();
```

- `code` - the status code as a number
- `name` - the name of the error as a "bumpy case", i.e. `NotFound` or `InternalServerError`.

[npm-image]: https://img.shields.io/npm/v/http-error.svg?style=flat-square
[npm-url]: https://npmjs.org/package/http-error
[travis-image]: https://img.shields.io/travis/jonathanong/http-error.svg?style=flat-square
[travis-url]: https://travis-ci.org/jonathanong/http-error
[coveralls-image]: https://img.shields.io/coveralls/jonathanong/http-error.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/jonathanong/http-error?branch=master
[david-image]: http://img.shields.io/david/jonathanong/http-error.svg?style=flat-square
[david-url]: https://david-dm.org/jonathanong/http-error
[license-image]: http://img.shields.io/npm/l/http-error.svg?style=flat-square
[license-url]: LICENSE
[downloads-image]: http://img.shields.io/npm/dm/http-error.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/http-error
66 changes: 66 additions & 0 deletions index.js
@@ -0,0 +1,66 @@

var assert = require('assert');
var statuses = require('statuses');
var inherits = require('util').inherits;

exports = module.exports = function (status, msg, props) {
if ('object' == typeof status && !(status instanceof Error)) {
props = status;
status = null;
}

// create(msg, status)
// this should be removed, but remains for koa backwards compat
if ('number' == typeof msg) {
var tmp = msg;
msg = status || statuses[tmp];
status = tmp;
}

props = props || {};
var err = msg instanceof Error
? msg
: new Error(msg || statuses[status || 500]);
for (var key in props) err[key] = props[key];
err.status = err.statusCode = status || err.status || 500;
assert(statuses(err.status), 'invalid status code');
err.expose = 'number' === typeof err.status
&& statuses[err.status]
&& err.status < 500;
return err;
};

// create generic error objects
var codes = statuses.codes.filter(function (num) {
return num >= 400;
});

codes.forEach(function (code) {
if (code >= 500) {
var ServerError = function ServerError(msg) {
Error.call(this);
this.message = msg || statuses[code];
Error.captureStackTrace(this, arguments.callee);
}
inherits(ServerError, Error);
ServerError.prototype.status =
ServerError.prototype.statusCode = code;
ServerError.prototype.expose = false;
exports[code] =
exports[statuses[code].replace(/\s+/g, '')] = ServerError;
return;
}

var ClientError = function ClientError(msg) {
Error.call(this);
this.message = msg || statuses[code];
Error.captureStackTrace(this, arguments.callee);
}
inherits(ClientError, Error);
ClientError.prototype.status =
ClientError.prototype.statusCode = code;
ClientError.prototype.expose = false;
exports[code] =
exports[statuses[code].replace(/\s+/g, '')] = ClientError;
return;
});
33 changes: 33 additions & 0 deletions package.json
@@ -0,0 +1,33 @@
{
"name": "http-errors",
"description": "Create HTTP error objects",
"version": "0.0.0",
"author": {
"name": "Jonathan Ong",
"email": "me@jongleberry.com",
"url": "http://jongleberry.com",
"twitter": "https://twitter.com/jongleberry"
},
"license": "MIT",
"repository": "jshttp/http-errors",
"dependencies": {
"statuses": "~1.0.4"
},
"devDependencies": {
"istanbul": "0",
"mocha": "1"
},
"scripts": {
"test": "mocha --reporter spec",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot"
},
"keywords": [
"http",
"error"
],
"files": [
"index.js",
"LICENSE"
]
}
72 changes: 72 additions & 0 deletions test/test.js
@@ -0,0 +1,72 @@

var assert = require('assert');

var create = require('..');

describe('HTTP Errors', function () {
it('create(status)', function () {
var err = create(404);
assert.equal(err.message, 'Not Found');
assert.equal(err.status, 404);
assert.equal(err.statusCode, 404);
})

it('create(status, msg)', function () {
var err = create(404, 'LOL');
assert.equal(err.message, 'LOL');
assert.equal(err.status, 404);
assert.equal(err.statusCode, 404);
})

it('create(props)', function () {
var err = create({
id: 1
});
assert.equal(err.id, 1);
assert.equal(err.message, 'Internal Server Error');
assert.equal(err.status, 500);
assert.equal(err.statusCode, 500);
})

it('create(msg, status)', function () {
var err = create('LOL', 404);
assert.equal(err.message, 'LOL');
assert.equal(err.status, 404);
assert.equal(err.statusCode, 404);
})

it('create(status, err, props)', function () {
var _err = new Error('LOL');
var err = create(404, _err, {
id: 1
});
assert.equal(err, _err);
assert.equal(err.message, 'LOL');
assert.equal(err.status, 404);
assert.equal(err.statusCode, 404);
})

it('new create.NotFound()', function () {
var err = new create.NotFound();
assert.equal(err.message, 'Not Found');
assert.equal(err.status, 404);
assert.equal(err.statusCode, 404);
assert(err.stack);
})

it('new create.InternalServerError()', function () {
var err = new create.InternalServerError();
assert.equal(err.message, 'Internal Server Error');
assert.equal(err.status, 500);
assert.equal(err.statusCode, 500);
assert(err.stack);
})

it('new create["404"]()', function () {
var err = new create['404']();
assert.equal(err.message, 'Not Found');
assert.equal(err.status, 404);
assert.equal(err.statusCode, 404);
assert(err.stack);
})
})

0 comments on commit 4ce1368

Please sign in to comment.