Comparing version 1.0.0-rc.0 to 1.0.0-rc.1
15
index.js
'use strict' | ||
const assert = require('assert') | ||
module.exports = createCustomError | ||
@@ -8,3 +10,10 @@ | ||
assert.equal(typeof name, 'string', 'constructor name must be a string') | ||
assert(name, 'constructor name cannot be an empty string') | ||
if (init) | ||
assert.equal(typeof init, 'function', 'constructor must be a function') | ||
assert(parent === Error || parent.prototype instanceof Error, 'parent must be derived from Error()') | ||
const ctor = function () { | ||
// make `new` optional | ||
if (this instanceof ctor) | ||
@@ -15,3 +24,5 @@ var inst = this | ||
// create base error object | ||
const err = parent.apply(inst, arguments) | ||
// get rid of irrelevant stack frames | ||
Error.captureStackTrace(err, ctor) | ||
@@ -22,3 +33,3 @@ | ||
// stack is not always relevant, and is expensive to generate | ||
// stack is not always important and is expensive to generate | ||
Object.defineProperty(inst, 'stack', { | ||
@@ -35,2 +46,3 @@ get: () => err.stack, | ||
// set function name (it appears in traces) | ||
Object.defineProperty(ctor, 'name', { | ||
@@ -41,2 +53,3 @@ value: name, | ||
// inherit from parent | ||
ctor.prototype = Object.create(parent.prototype) | ||
@@ -43,0 +56,0 @@ ctor.prototype.inspect = inspect |
{ | ||
"name": "cer", | ||
"version": "1.0.0-rc.0", | ||
"version": "1.0.0-rc.1", | ||
"description": "create fast and correct error constructors", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,2 +10,2 @@ [![view on npm](http://img.shields.io/npm/v/cer.svg?style=flat-square)](https://www.npmjs.com/package/cer) | ||
create correct custom error constructors | ||
create fast and correct error constructors |
23
test.js
'use strict' | ||
const inspect = require('util').inspect, | ||
const AE = require('assert').AssertionError, | ||
inspect = require('util').inspect, | ||
test = require('tap'), | ||
@@ -67,2 +68,22 @@ error = require('.'), | ||
test.test('assertions', test => { | ||
test.throws(() => { | ||
error(true, init, Error) | ||
}, AE, 'constructor name should be asserted') | ||
test.throws(() => { | ||
error('', init, Error) | ||
}, AE, 'constructor name should be asserted') | ||
test.throws(() => { | ||
error('Invalid', true, MyError1) | ||
}, AE, 'constructor type should be asserted') | ||
test.throws(() => { | ||
error('Invalid', null, init) | ||
}, AE, 'prototype chain of parent should be asserted') | ||
test.end() | ||
}) | ||
test.test('multiple inheritance', test => { | ||
@@ -69,0 +90,0 @@ function init(message, val) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9984
143