err-code
Advanced tools
Comparing version 2.0.1 to 2.0.2
{ | ||
"root": true, | ||
"extends": [ | ||
"@satazor/eslint-config/es5", | ||
"@satazor/eslint-config/es6", | ||
"@satazor/eslint-config/addons/node" | ||
] | ||
} |
48
index.js
'use strict'; | ||
function maybeDefineProperty(obj, name, value) { | ||
var descriptor; | ||
if (Object.isFrozen(obj)) { | ||
return; | ||
function assign(obj, props) { | ||
for (const key in props) { | ||
Object.defineProperty(obj, key, { | ||
value: props[key], | ||
enumerable: true, | ||
configurable: true, | ||
}); | ||
} | ||
descriptor = Object.getOwnPropertyDescriptor(obj, name); | ||
if (descriptor && !descriptor.writable) { | ||
return; | ||
} | ||
obj[name] = value; | ||
return obj; | ||
} | ||
function createError(err, code, props) { | ||
var key; | ||
if (!(err instanceof Error)) { | ||
@@ -26,17 +20,29 @@ throw new TypeError('Please pass an Error to err-code'); | ||
if (!props) { | ||
props = {}; | ||
} | ||
if (typeof code === 'object') { | ||
props = code; | ||
} else if (code != null) { | ||
maybeDefineProperty(err, 'code', code); | ||
code = undefined; | ||
} | ||
if (props) { | ||
for (key in props) { | ||
maybeDefineProperty(err, key, props[key]); | ||
} | ||
if (code != null) { | ||
props.code = code; | ||
} | ||
return err; | ||
try { | ||
return assign(err, props); | ||
} catch (_) { | ||
props.message = err.message; | ||
props.stack = err.stack; | ||
const ErrClass = function () {}; | ||
ErrClass.prototype = Object.create(Object.getPrototypeOf(err)); | ||
return assign(new ErrClass(), props); | ||
} | ||
} | ||
module.exports = createError; |
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.errCode = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
'use strict'; | ||
function maybeDefineProperty(obj, name, value) { | ||
var descriptor; | ||
if (Object.isFrozen(obj)) { | ||
return; | ||
function assign(obj, props) { | ||
for (const key in props) { | ||
Object.defineProperty(obj, key, { | ||
value: props[key], | ||
enumerable: true, | ||
configurable: true, | ||
}); | ||
} | ||
descriptor = Object.getOwnPropertyDescriptor(obj, name); | ||
if (descriptor && !descriptor.writable) { | ||
return; | ||
} | ||
obj[name] = value; | ||
return obj; | ||
} | ||
function createError(err, code, props) { | ||
var key; | ||
if (!(err instanceof Error)) { | ||
@@ -27,15 +21,27 @@ throw new TypeError('Please pass an Error to err-code'); | ||
if (!props) { | ||
props = {}; | ||
} | ||
if (typeof code === 'object') { | ||
props = code; | ||
} else if (code != null) { | ||
maybeDefineProperty(err, 'code', code); | ||
code = undefined; | ||
} | ||
if (props) { | ||
for (key in props) { | ||
maybeDefineProperty(err, key, props[key]); | ||
} | ||
if (code != null) { | ||
props.code = code; | ||
} | ||
return err; | ||
try { | ||
return assign(err, props); | ||
} catch (_) { | ||
props.message = err.message; | ||
props.stack = err.stack; | ||
const ErrClass = function () {}; | ||
ErrClass.prototype = Object.create(Object.getPrototypeOf(err)); | ||
return assign(new ErrClass(), props); | ||
} | ||
} | ||
@@ -42,0 +48,0 @@ |
{ | ||
"name": "err-code", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Create an error with a code", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -60,3 +60,3 @@ # err-code | ||
If the passed `Error` already has a `.code` field, or fields specified in the third argument to `errcode` they will be overwritten, unless the fields have been [defined as read-only](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) or the `Error` object has been [frozen](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze). | ||
If the passed `Error` already has a `.code` field, or fields specified in the third argument to `errcode` they will be overwritten, unless the fields are read only or otherwise throw during assignment in which case a new object will be created that shares a prototype chain with the original `Error`. The `.stack` and `.message` properties will be carried over from the original error and `.code` or any passed properties will be set on it. | ||
@@ -63,0 +63,0 @@ |
128
test/test.js
'use strict'; | ||
var errcode = require('../index'); | ||
var expect = require('expect.js'); | ||
const errcode = require('../index'); | ||
const expect = require('expect.js'); | ||
describe('errcode', function () { | ||
describe('string as first argument', function () { | ||
it('should throw an error', function () { | ||
expect(function () { errcode('my message'); }).to.throwError(function (err) { | ||
describe('errcode', () => { | ||
describe('string as first argument', () => { | ||
it('should throw an error', () => { | ||
expect(() => { errcode('my message'); }).to.throwError((err) => { | ||
expect(err).to.be.a(TypeError); | ||
@@ -15,6 +15,6 @@ }); | ||
describe('error as first argument', function () { | ||
it('should accept an error and do nothing', function () { | ||
var myErr = new Error('my message'); | ||
var err = errcode(myErr); | ||
describe('error as first argument', () => { | ||
it('should accept an error and do nothing', () => { | ||
const myErr = new Error('my message'); | ||
const err = errcode(myErr); | ||
@@ -25,5 +25,5 @@ expect(err).to.be(myErr); | ||
it('should accept an error and add a code', function () { | ||
var myErr = new Error('my message'); | ||
var err = errcode(myErr, 'ESOME'); | ||
it('should accept an error and add a code', () => { | ||
const myErr = new Error('my message'); | ||
const err = errcode(myErr, 'ESOME'); | ||
@@ -34,5 +34,5 @@ expect(err).to.be(myErr); | ||
it('should accept an error object and add code & properties', function () { | ||
var myErr = new Error('my message'); | ||
var err = errcode(myErr, 'ESOME', { foo: 'bar', bar: 'foo' }); | ||
it('should accept an error object and add code & properties', () => { | ||
const myErr = new Error('my message'); | ||
const err = errcode(myErr, 'ESOME', { foo: 'bar', bar: 'foo' }); | ||
@@ -45,5 +45,5 @@ expect(err).to.be.an(Error); | ||
it('should create an error object without code but with properties', function () { | ||
var myErr = new Error('my message'); | ||
var err = errcode(myErr, { foo: 'bar', bar: 'foo' }); | ||
it('should create an error object without code but with properties', () => { | ||
const myErr = new Error('my message'); | ||
const err = errcode(myErr, { foo: 'bar', bar: 'foo' }); | ||
@@ -56,5 +56,4 @@ expect(err).to.be.an(Error); | ||
it('should not attempt to set non-writable field', function () { | ||
var myErr = new Error('my message'); | ||
var err; | ||
it('should set a non-writable field', () => { | ||
const myErr = new Error('my message'); | ||
@@ -65,20 +64,83 @@ Object.defineProperty(myErr, 'code', { | ||
}); | ||
err = errcode(myErr, 'ERR_WAT'); | ||
const err = errcode(myErr, 'ERR_WAT'); | ||
expect(err).to.be.an(Error); | ||
expect(err.code).to.be('derp'); | ||
expect(err.stack).to.equal(myErr.stack); | ||
expect(err.code).to.be('ERR_WAT'); | ||
}); | ||
it('should not add code to frozen object', function () { | ||
var myErr = new Error('my message'); | ||
var err = errcode(Object.freeze(myErr), 'ERR_WAT'); | ||
it('should add a code to frozen object', () => { | ||
const myErr = new Error('my message'); | ||
const err = errcode(Object.freeze(myErr), 'ERR_WAT'); | ||
expect(err).to.be.an(Error); | ||
expect(err.code).to.be(undefined); | ||
expect(err.stack).to.equal(myErr.stack); | ||
expect(err.code).to.be('ERR_WAT'); | ||
}); | ||
it('should to set a field that throws at assignment time', () => { | ||
const myErr = new Error('my message'); | ||
Object.defineProperty(myErr, 'code', { | ||
enumerable: true, | ||
set() { | ||
throw new Error('Nope!'); | ||
}, | ||
get() { | ||
return 'derp'; | ||
}, | ||
}); | ||
const err = errcode(myErr, 'ERR_WAT'); | ||
expect(err).to.be.an(Error); | ||
expect(err.stack).to.equal(myErr.stack); | ||
expect(err.code).to.be('ERR_WAT'); | ||
}); | ||
it('should retain error type', () => { | ||
const myErr = new TypeError('my message'); | ||
Object.defineProperty(myErr, 'code', { | ||
value: 'derp', | ||
writable: false, | ||
}); | ||
const err = errcode(myErr, 'ERR_WAT'); | ||
expect(err).to.be.a(TypeError); | ||
expect(err.stack).to.equal(myErr.stack); | ||
expect(err.code).to.be('ERR_WAT'); | ||
}); | ||
it('should add a code to a class that extends Error', () => { | ||
class CustomError extends Error { | ||
set code(val) { | ||
throw new Error('Nope!'); | ||
} | ||
} | ||
const myErr = new CustomError('my message'); | ||
Object.defineProperty(myErr, 'code', { | ||
value: 'derp', | ||
writable: false, | ||
configurable: false, | ||
}); | ||
const err = errcode(myErr, 'ERR_WAT'); | ||
expect(err).to.be.a(CustomError); | ||
expect(err.stack).to.equal(myErr.stack); | ||
expect(err.code).to.be('ERR_WAT'); | ||
// original prototype chain should be intact | ||
expect(() => { | ||
const otherErr = new CustomError('my message'); | ||
otherErr.code = 'derp'; | ||
}).to.throwError(); | ||
}); | ||
}); | ||
describe('falsy first arguments', function () { | ||
it('should not allow passing null as the first argument', function () { | ||
expect(function () { errcode(null); }).to.throwError(function (err) { | ||
describe('falsy first arguments', () => { | ||
it('should not allow passing null as the first argument', () => { | ||
expect(() => { errcode(null); }).to.throwError((err) => { | ||
expect(err).to.be.a(TypeError); | ||
@@ -88,4 +150,4 @@ }); | ||
it('should not allow passing undefined as the first argument', function () { | ||
expect(function () { errcode(undefined); }).to.throwError(function (err) { | ||
it('should not allow passing undefined as the first argument', () => { | ||
expect(() => { errcode(undefined); }).to.throwError((err) => { | ||
expect(err).to.be.a(TypeError); | ||
@@ -92,0 +154,0 @@ }); |
11990
239