create-boom-error
Advanced tools
Comparing version 2.0.0 to 3.0.0
72
index.js
@@ -1,50 +0,44 @@ | ||
'use strict'; | ||
const Boom = require('@hapi/boom'); | ||
const Boom = require('@hapi/boom'); | ||
const Decamelize = require('decamelize'); | ||
/** | ||
* | ||
* @param {string} name | ||
* @param {number} statusCode | ||
* @param {function | string} message | ||
* @param {string} code | ||
* @returns {new () => Boom<null>} | ||
*/ | ||
class BaseError extends Error { | ||
name; | ||
code; | ||
} | ||
function isDecoratedBoom(err) { | ||
return err instanceof BaseError; | ||
} | ||
function createBoomError(name, statusCode, message, code) { | ||
var exports = this; | ||
return class extends BaseError { | ||
constructor() { | ||
super(); | ||
function ErrorCtor () { | ||
this.name = name; | ||
if (code) { | ||
this.code = code; | ||
} else { | ||
this.code = Decamelize(name); | ||
} | ||
this.name = name; | ||
if (code) { | ||
this.code = code; | ||
} else { | ||
this.code = Decamelize(name); | ||
} | ||
this.message = undefined; | ||
if (typeof message === 'string') { | ||
this.message = message; | ||
} else if (typeof message === 'function') { | ||
this.message = message.apply(null, arguments); | ||
} | ||
this.message = undefined; | ||
if (typeof message === 'string') { | ||
this.message = message; | ||
} else if (typeof message === 'function') { | ||
this.message = message.apply(null, arguments); | ||
} | ||
Boom.boomify(this, { statusCode }); | ||
Boom.boomify(this, { statusCode }); | ||
if (!message) { | ||
Reflect.deleteProperty(this.output.payload, 'message'); | ||
if (!message) { | ||
Reflect.deleteProperty(this.output.payload, 'message'); | ||
} | ||
} | ||
}; | ||
ErrorCtor.prototype = Object.create(Error.prototype); | ||
ErrorCtor.prototype.constructor = ErrorCtor; | ||
ErrorCtor.prototype.name = name; | ||
if (exports) { | ||
exports[name] = ErrorCtor; | ||
} | ||
return ErrorCtor; | ||
} | ||
module.exports = createBoomError; | ||
module.exports = { | ||
isDecoratedBoom, | ||
createBoomError | ||
}; |
{ | ||
"name": "create-boom-error", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Simply create sub-classed Boom errors for Hapi applications.", | ||
@@ -24,3 +24,4 @@ "main": "index.js", | ||
], | ||
"author": "Marcus Gartner", | ||
"types": "index.d.ts", | ||
"author": "Lob Engineering <engineering@lob.com>", | ||
"license": "MIT", | ||
@@ -27,0 +28,0 @@ "bugs": { |
@@ -7,3 +7,3 @@ # create-boom-error [![npm version](https://badge.fury.io/js/create-boom-error.svg)](http://badge.fury.io/js/create-boom-error) [![Build Status](https://travis-ci.org/lob/create-boom-error.svg)](https://travis-ci.org/lob/create-boom-error) | ||
`yarn add create-boom-error` | ||
`npm install create-boom-error` | ||
@@ -15,2 +15,3 @@ # Usage | ||
Creates a Boom error. | ||
- `name` - The name of the error. | ||
@@ -24,7 +25,7 @@ - `statusCode` - the integer status code of the Boom error | ||
```js | ||
var createBoomError = require('create-boom-error'); | ||
const { createBoomError } = require('create-boom-error'); | ||
var MyError = createBoomError('MyError', 404, 'simple message', 'not_found'); | ||
const MyError = createBoomError('MyError', 404, 'simple message', 'not_found'); | ||
var err = new MyError(); | ||
const err = new MyError(); | ||
@@ -56,7 +57,5 @@ // err is an instance of MyError making it easy to check in tests | ||
```js | ||
var MyError = createBoomError('MyError', 404, function (num) { | ||
return 'You must have more than ' + num + ' coins.'; | ||
}); | ||
const MyError = createBoomError('MyError', 404, (num) => `You must have more than ${num} coins.`); | ||
var err = new MyError(4); | ||
const err = new MyError(4); | ||
@@ -67,24 +66,2 @@ // err now has the dynamic message | ||
### Automatically exporting an error | ||
This is a useful shortcut if you have a file in your application where you want to declare all your errors and automatically export them. Simply call `.bind(exports)` when requiring the create-boom-error library. | ||
```js | ||
// in customErrors.js | ||
var createBoomError = require('create-boom-error').bind(exports); | ||
createBoomError('TestError', 400, 'test message'); | ||
``` | ||
This error is automatically exported in `customError.js`: | ||
```js | ||
// in another file | ||
var CustomErrors = require('./customErrors'); | ||
var err = new CustomErrors.TestError(); | ||
err instanceof CustomErrors.TestError // => true | ||
``` | ||
# Development | ||
@@ -94,2 +71,2 @@ | ||
`yarn test` | ||
`npm run test` |
'use strict'; | ||
var expect = require('chai').expect; | ||
var createBoomError = require('../index.js'); | ||
var CustomErrors = require('./customErrors'); | ||
const expect = require('chai').expect; | ||
const { createBoomError, isDecoratedBoom } = require('../index.js'); | ||
describe('createBoomError', function () { | ||
describe('create-boom-error', () => { | ||
it('should create a boom error with a string message', function () { | ||
var StringError = createBoomError('StringError', 404, 'string message'); | ||
var err = new StringError(); | ||
it('checks if the error subclasses the base error class', () => { | ||
const StringError = createBoomError('StringError', 404, 'string message'); | ||
const err = new StringError(); | ||
const regularErr = new Error(); | ||
expect(isDecoratedBoom(err)).to.be.true; | ||
expect(isDecoratedBoom(regularErr)).to.be.false; | ||
}); | ||
it('should create a boom error with a string message', () => { | ||
const StringError = createBoomError('StringError', 404, 'string message'); | ||
const err = new StringError(); | ||
expect(err instanceof StringError).to.be.true; | ||
@@ -27,7 +36,6 @@ expect(err instanceof Error).to.be.true; | ||
it('should create a boom error with a function message', function () { | ||
var FunctionError = createBoomError('FunctionError', 422, function (value) { | ||
return 'value is ' + value; | ||
}); | ||
var err = new FunctionError('one'); | ||
it('should create a boom error with a function message', () => { | ||
const FunctionError = createBoomError('FunctionError', 422, (value) => `value is ${value}`); | ||
const err = new FunctionError('one'); | ||
expect(err instanceof FunctionError).to.be.true; | ||
@@ -49,4 +57,5 @@ expect(err instanceof Error).to.be.true; | ||
it('should create a boom error without a message', function () { | ||
var EmptyError = createBoomError('EmptyError', 422); | ||
var err = new EmptyError(); | ||
const EmptyError = createBoomError('EmptyError', 422); | ||
const err = new EmptyError(); | ||
expect(err instanceof EmptyError).to.be.true; | ||
@@ -69,2 +78,3 @@ expect(err instanceof Error).to.be.true; | ||
const err = new CodeError(); | ||
expect(err instanceof CodeError).to.be.true; | ||
@@ -82,13 +92,2 @@ expect(err instanceof Error).to.be.true; | ||
}); | ||
it('should create a boom string message error on exports', function () { | ||
var err = new CustomErrors.StringError(); | ||
expect(err instanceof CustomErrors.StringError).to.be.true; | ||
}); | ||
it('should create a boom function message error on exports', function () { | ||
var err = new CustomErrors.FunctionError(); | ||
expect(err instanceof CustomErrors.FunctionError).to.be.true; | ||
}); | ||
}); |
9645
120
67