diff options
author | Gar <gar+gh@danger.computer> | 2021-02-12 02:05:30 +0300 |
---|---|---|
committer | Gar <gar+gh@danger.computer> | 2021-02-12 02:05:30 +0300 |
commit | e998811170ce5df00a725b2d683b4bff124c6792 (patch) | |
tree | 9d9e56247e8bb166f94f6481dd348065ff783b70 /node_modules/err-code | |
parent | 6305ebde43796737014aedbe019db8cd81dcbbec (diff) |
libnpmversion@1.0.10
Diffstat (limited to 'node_modules/err-code')
-rw-r--r-- | node_modules/err-code/.eslintrc.json | 2 | ||||
-rw-r--r-- | node_modules/err-code/.npmignore | 2 | ||||
-rw-r--r-- | node_modules/err-code/.travis.yml | 3 | ||||
-rw-r--r-- | node_modules/err-code/README.md | 26 | ||||
-rw-r--r-- | node_modules/err-code/index.js | 45 | ||||
-rw-r--r-- | node_modules/err-code/index.umd.js | 49 | ||||
-rw-r--r-- | node_modules/err-code/package.json | 8 | ||||
-rw-r--r-- | node_modules/err-code/test/test.js | 167 |
8 files changed, 207 insertions, 95 deletions
diff --git a/node_modules/err-code/.eslintrc.json b/node_modules/err-code/.eslintrc.json index f34b41d96..4829595a4 100644 --- a/node_modules/err-code/.eslintrc.json +++ b/node_modules/err-code/.eslintrc.json @@ -1,7 +1,7 @@ { "root": true, "extends": [ - "@satazor/eslint-config/es5", + "@satazor/eslint-config/es6", "@satazor/eslint-config/addons/node" ] }
\ No newline at end of file diff --git a/node_modules/err-code/.npmignore b/node_modules/err-code/.npmignore deleted file mode 100644 index a22dcdd47..000000000 --- a/node_modules/err-code/.npmignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules -npm-debug.* diff --git a/node_modules/err-code/.travis.yml b/node_modules/err-code/.travis.yml index f85680370..b29cf66a2 100644 --- a/node_modules/err-code/.travis.yml +++ b/node_modules/err-code/.travis.yml @@ -1,5 +1,4 @@ language: node_js node_js: - - "0.12" - "4" - - "5" + - "6" diff --git a/node_modules/err-code/README.md b/node_modules/err-code/README.md index e0234e90a..5afdab00c 100644 --- a/node_modules/err-code/README.md +++ b/node_modules/err-code/README.md @@ -1,6 +1,6 @@ # err-code -[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url] +[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url] [![Greenkeeper badge][greenkeeper-image]][greenkeeper-url] [npm-url]:https://npmjs.org/package/err-code [downloads-image]:http://img.shields.io/npm/dm/err-code.svg @@ -9,16 +9,21 @@ [travis-image]:http://img.shields.io/travis/IndigoUnited/js-err-code/master.svg [david-dm-url]:https://david-dm.org/IndigoUnited/js-err-code [david-dm-image]:https://img.shields.io/david/IndigoUnited/js-err-code.svg -[david-dm-dev-url]:https://david-dm.org/IndigoUnited/js-err-code#info=devDependencies +[david-dm-dev-url]:https://david-dm.org/IndigoUnited/js-err-code?type=dev [david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/js-err-code.svg +[greenkeeper-image]:https://badges.greenkeeper.io/IndigoUnited/js-err-code.svg +[greenkeeper-url]:https://greenkeeper.io/ Create new error instances with a code and additional properties. ## Installation -`$ npm install err-code` - `NPM` -`$ bower install err-code` - `bower` +```console +$ npm install err-code +// or +$ bower install err-code +``` The browser file is named index.umd.js which supports CommonJS, AMD and globals (errCode). @@ -48,18 +53,11 @@ throw errcode(new Error('My message'), 'ESOMECODE'); throw errcode(new Error('My message'), 'ESOMECODE', { detail: 'Additional information about the error' }); // fill error with message + props throw errcode(new Error('My message'), { detail: 'Additional information about the error' }); +``` +## Pre-existing fields -// You may also pass a string in the first argument and an error will be automatically created -// for you, though the stack trace will contain err-code in it. - -// create error with message + code -throw errcode('My message', 'ESOMECODE'); -// create error with message + code + props -throw errcode('My message', 'ESOMECODE', { detail: 'Additional information about the error' }); -// create error with message + props -throw errcode('My message', { detail: 'Additional information about the error' }); -``` +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. ## Tests diff --git a/node_modules/err-code/index.js b/node_modules/err-code/index.js index 1a0a69f62..9ff3e9c5d 100644 --- a/node_modules/err-code/index.js +++ b/node_modules/err-code/index.js @@ -1,22 +1,47 @@ 'use strict'; -function createError(msg, code, props) { - var err = msg instanceof Error ? msg : new Error(msg); - var key; +function assign(obj, props) { + for (const key in props) { + Object.defineProperty(obj, key, { + value: props[key], + enumerable: true, + configurable: true, + }); + } + + return obj; +} + +function createError(err, code, props) { + if (!err || typeof err === 'string') { + throw new TypeError('Please pass an Error to err-code'); + } + + if (!props) { + props = {}; + } if (typeof code === 'object') { props = code; - } else if (code != null) { - err.code = code; + code = undefined; } - if (props) { - for (key in props) { - 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; diff --git a/node_modules/err-code/index.umd.js b/node_modules/err-code/index.umd.js index d1dbdf733..41007269d 100644 --- a/node_modules/err-code/index.umd.js +++ b/node_modules/err-code/index.umd.js @@ -1,26 +1,51 @@ -(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 e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ +(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 createError(msg, code, props) { - var err = msg instanceof Error ? msg : new Error(msg); - var key; +function assign(obj, props) { + for (const key in props) { + Object.defineProperty(obj, key, { + value: props[key], + enumerable: true, + configurable: true, + }); + } + + return obj; +} + +function createError(err, code, props) { + if (!err || typeof err === 'string') { + throw new TypeError('Please pass an Error to err-code'); + } + + if (!props) { + props = {}; + } if (typeof code === 'object') { props = code; - } else if (code != null) { - err.code = code; + code = undefined; } - if (props) { - for (key in props) { - 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; },{}]},{},[1])(1) -});
\ No newline at end of file +}); diff --git a/node_modules/err-code/package.json b/node_modules/err-code/package.json index 705ec0e1d..8c6007b39 100644 --- a/node_modules/err-code/package.json +++ b/node_modules/err-code/package.json @@ -1,6 +1,6 @@ { "name": "err-code", - "version": "1.1.2", + "version": "2.0.3", "description": "Create an error with a code", "main": "index.js", "scripts": { @@ -26,9 +26,9 @@ "license": "MIT", "devDependencies": { "@satazor/eslint-config": "^3.0.0", - "browserify": "^14.0.0", - "eslint": "^3.0.0", + "browserify": "^16.5.1", + "eslint": "^7.2.0", "expect.js": "^0.3.1", - "mocha": "^3.0.2" + "mocha": "^8.0.1" } } diff --git a/node_modules/err-code/test/test.js b/node_modules/err-code/test/test.js index 81b2b26d3..22ba0a8a1 100644 --- a/node_modules/err-code/test/test.js +++ b/node_modules/err-code/test/test.js @@ -1,26 +1,37 @@ 'use strict'; -var errcode = require('../index'); -var expect = require('expect.js'); +const errcode = require('../index'); +const expect = require('expect.js'); + +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); + }); + }); + }); -describe('errcode', function () { - describe('string as first argument', function () { - it('should create an error object without code', function () { - var err = errcode('my message'); + describe('error as first argument', () => { + it('should accept an error and do nothing', () => { + const myErr = new Error('my message'); + const err = errcode(myErr); - expect(err).to.be.an(Error); + expect(err).to.be(myErr); expect(err.hasOwnProperty(err.code)).to.be(false); }); - it('should create an error object with code', function () { - var err = errcode('my message', 'ESOME'); + it('should accept an error and add a code', () => { + const myErr = new Error('my message'); + const err = errcode(myErr, 'ESOME'); - expect(err).to.be.an(Error); + expect(err).to.be(myErr); expect(err.code).to.be('ESOME'); }); - it('should create an error object with code and properties', function () { - var err = errcode('my message', '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' }); expect(err).to.be.an(Error); expect(err.code).to.be('ESOME'); @@ -28,65 +39,121 @@ describe('errcode', function () { expect(err.bar).to.be('foo'); }); - it('should create an error object without code but with properties', function () { - var err = errcode('my message', { 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' }); expect(err).to.be.an(Error); expect(err.code).to.be(undefined); expect(err.foo).to.be('bar'); expect(err.bar).to.be('foo'); }); - }); - 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); + it('should set a non-writable field', () => { + const myErr = new Error('my message'); - expect(err).to.be(myErr); - expect(err.hasOwnProperty(err.code)).to.be(false); + Object.defineProperty(myErr, 'code', { + value: 'derp', + writable: false, + }); + 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 accept an error and add a code', function () { - var myErr = new Error('my message'); - var err = errcode(myErr, 'ESOME'); + 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(myErr); - expect(err.code).to.be('ESOME'); + expect(err).to.be.an(Error); + expect(err.stack).to.equal(myErr.stack); + expect(err.code).to.be('ERR_WAT'); }); - 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 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.code).to.be('ESOME'); - expect(err.foo).to.be('bar'); - expect(err.bar).to.be('foo'); + expect(err.stack).to.equal(myErr.stack); + expect(err.code).to.be('ERR_WAT'); }); - 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 retain error type', () => { + const myErr = new TypeError('my message'); - expect(err).to.be.an(Error); - expect(err.code).to.be(undefined); - expect(err.foo).to.be('bar'); - expect(err.bar).to.be('foo'); + 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 allow passing null & undefined in the first argument', function () { - var err; + 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'); - err = errcode(null, 'ESOME'); - expect(err).to.be.an(Error); - expect(err.message).to.be('null'); - expect(err.code).to.be('ESOME'); + // original prototype chain should be intact + expect(() => { + const otherErr = new CustomError('my message'); - err = errcode(undefined, 'ESOME'); - expect(err).to.be.an(Error); - expect(err.message).to.be(''); - expect(err.code).to.be('ESOME'); + otherErr.code = 'derp'; + }).to.throwError(); + }); + + it('should support errors that are not Errors', () => { + const err = errcode({ + message: 'Oh noes!', + }, 'ERR_WAT'); + + expect(err.message).to.be('Oh noes!'); + expect(err.code).to.be('ERR_WAT'); + }); + }); + + 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); + }); + }); + + it('should not allow passing undefined as the first argument', () => { + expect(() => { errcode(undefined); }).to.throwError((err) => { + expect(err).to.be.a(TypeError); + }); + }); }); }); |