Comparing version 4.0.1 to 5.0.0
@@ -1,45 +0,41 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _extendableBuiltin(cls) { | ||
function ExtendableBuiltin() { | ||
var instance = Reflect.construct(cls, Array.from(arguments)); | ||
Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); | ||
return instance; | ||
} | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
ExtendableBuiltin.prototype = Object.create(cls.prototype, { | ||
constructor: { | ||
value: cls, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
if (Object.setPrototypeOf) { | ||
Object.setPrototypeOf(ExtendableBuiltin, cls); | ||
} else { | ||
ExtendableBuiltin.__proto__ = cls; | ||
} | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
return ExtendableBuiltin; | ||
} | ||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } | ||
var os = require('os'); | ||
function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } | ||
// Create an Error able to contain some params and better stack traces | ||
function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } | ||
var YError = function (_extendableBuiltin2) { | ||
_inherits(YError, _extendableBuiltin2); | ||
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
var os = require('os'); // Create an Error able to contain some params and better stack traces | ||
var YError = | ||
/*#__PURE__*/ | ||
function (_Error) { | ||
_inherits(YError, _Error); | ||
function YError(wrappedErrors, errorCode) { | ||
for (var _len = arguments.length, params = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
var _this; | ||
for (var _len = arguments.length, params = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
params[_key - 2] = arguments[_key]; | ||
@@ -55,8 +51,6 @@ } | ||
wrappedErrors = []; | ||
} | ||
} // Call the parent constructor | ||
// Call the parent constructor | ||
// Filling error | ||
var _this = _possibleConstructorReturn(this, (YError.__proto__ || Object.getPrototypeOf(YError)).call(this, errorCode)); | ||
_this = _possibleConstructorReturn(this, _getPrototypeOf(YError).call(this, errorCode)); // Filling error | ||
@@ -69,4 +63,5 @@ _this.code = errorCode || 'E_UNEXPECTED'; | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(_this, _this.constructor); | ||
Error.captureStackTrace(_assertThisInitialized(_this), _this.constructor); | ||
} | ||
return _this; | ||
@@ -76,6 +71,5 @@ } | ||
_createClass(YError, [{ | ||
key: 'toString', | ||
key: "toString", | ||
value: function toString() { | ||
return (this.wrappedErrors.length ? | ||
// eslint-disable-next-line | ||
return (this.wrappedErrors.length ? // eslint-disable-next-line | ||
this.wrappedErrors[this.wrappedErrors.length - 1].stack + os.EOL : '') + this.constructor.name + ': ' + this.code + ' (' + this.params.join(', ') + ')'; | ||
@@ -86,10 +80,10 @@ } | ||
return YError; | ||
}(_extendableBuiltin(Error)); | ||
}(_wrapNativeSuper(Error)); // Wrap a classic error | ||
// Wrap a classic error | ||
YError.wrap = function yerrorWrap(err, errorCode) { | ||
var yError = null; | ||
var wrappedErrorIsACode = _looksLikeAYErrorCode(err.message); | ||
var wrappedErrors = (err.wrappedErrors || []).concat(err); | ||
@@ -105,3 +99,3 @@ | ||
for (var _len2 = arguments.length, params = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { | ||
for (var _len2 = arguments.length, params = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { | ||
params[_key2 - 2] = arguments[_key2]; | ||
@@ -113,3 +107,4 @@ } | ||
} | ||
yError = new (Function.prototype.bind.apply(YError, [null].concat([wrappedErrors, errorCode], params)))(); | ||
yError = _construct(YError, [wrappedErrors, errorCode].concat(params)); | ||
return yError; | ||
@@ -123,3 +118,3 @@ }; | ||
for (var _len3 = arguments.length, params = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
for (var _len3 = arguments.length, params = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
params[_key3 - 1] = arguments[_key3]; | ||
@@ -136,3 +131,3 @@ } | ||
for (var _len4 = arguments.length, params = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { | ||
for (var _len4 = arguments.length, params = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { | ||
params[_key4 - 1] = arguments[_key4]; | ||
@@ -142,6 +137,6 @@ } | ||
return YError.wrap.apply(YError, [err].concat(params)); | ||
}; | ||
}; // In order to keep compatibility through major versions | ||
// we have to make kind of an cross major version instanceof | ||
// In order to keep compatibility through major versions | ||
// we have to make kind of an cross major version instanceof | ||
function _looksLikeAYError(err) { | ||
@@ -152,6 +147,5 @@ return err instanceof YError || err.constructor && err.constructor.name && err.constructor.name.endsWith('Error') && 'string' === typeof err.code && _looksLikeAYErrorCode(err.code) && err.params && err.params instanceof Array; | ||
function _looksLikeAYErrorCode(str) { | ||
return (/^([A-Z0-9_]+)$/.test(str) | ||
); | ||
return /^([A-Z0-9_]+)$/.test(str); | ||
} | ||
module.exports = YError; |
@@ -0,14 +1,12 @@ | ||
"use strict"; | ||
/* eslint max-nested-callbacks:[0], no-magic-numbers:[0] */ | ||
'use strict'; | ||
var assert = require('assert'); | ||
var assert = require('assert'); | ||
var YError = require('./index.js'); | ||
describe('YError', function () { | ||
describe('.__constructor', function () { | ||
it('Should work', function () { | ||
var err = new YError('E_ERROR', 'arg1', 'arg2'); | ||
assert(err instanceof Error); | ||
@@ -22,3 +20,2 @@ assert.equal(err.name, err.toString()); | ||
var err = new YError(); | ||
assert.equal(err.code, 'E_UNEXPECTED'); | ||
@@ -31,3 +28,2 @@ assert.deepEqual(err.params, []); | ||
var err = new YError('E_ERROR', 'arg1', 'arg2'); | ||
assert.equal(err.code, 'E_ERROR'); | ||
@@ -40,9 +36,6 @@ assert(err instanceof YError); | ||
}); | ||
describe('.wrap()', function () { | ||
it('Should work with standard errors and a message', function () { | ||
// eslint-disable-line | ||
var err = YError.wrap(new Error('This is an error!')); | ||
assert.equal(err.code, 'E_UNEXPECTED'); | ||
@@ -58,6 +51,4 @@ assert.equal(err.wrappedErrors.length, 1); | ||
}); | ||
it('Should work with standard errors and an error code', function () { | ||
var err = YError.wrap(new Error('E_ERROR')); | ||
assert.equal(err.code, 'E_ERROR'); | ||
@@ -71,8 +62,7 @@ assert.equal(err.wrappedErrors.length, 1); | ||
} | ||
assert.equal(err.name, err.toString()); | ||
}); | ||
it('Should work with standard errors, an error code and params', function () { | ||
var err = YError.wrap(new Error('E_ERROR'), 'E_ERROR_2', 'arg1', 'arg2'); | ||
assert.equal(err.code, 'E_ERROR_2'); | ||
@@ -86,8 +76,7 @@ assert.equal(err.wrappedErrors.length, 1); | ||
} | ||
assert.equal(err.name, err.toString()); | ||
}); | ||
it('Should work with several wrapped errors', function () { | ||
var err = YError.wrap(YError.wrap(new Error('E_ERROR_1'), 'E_ERROR_2', 'arg2.1', 'arg2.2'), 'E_ERROR_3', 'arg3.1', 'arg3.2'); | ||
assert.equal(err.code, 'E_ERROR_3'); | ||
@@ -102,11 +91,9 @@ assert.equal(err.wrappedErrors.length, 2); | ||
} | ||
assert.equal(err.name, err.toString()); | ||
}); | ||
}); | ||
describe('.cast()', function () { | ||
it('Should work with standard errors and a message', function () { | ||
var err = YError.cast(new Error('This is an error!')); | ||
assert.equal(err.code, 'E_UNEXPECTED'); | ||
@@ -120,8 +107,7 @@ assert.equal(err.wrappedErrors.length, 1); | ||
} | ||
assert.equal(err.name, err.toString()); | ||
}); | ||
it('Should let YError instances pass through', function () { | ||
var err = YError.cast(new YError('E_ERROR', 'arg1', 'arg2')); | ||
assert.equal(err.code, 'E_ERROR'); | ||
@@ -133,11 +119,9 @@ assert.deepEqual(err.params, ['arg1', 'arg2']); | ||
} | ||
assert.equal(err.name, err.toString()); | ||
}); | ||
}); | ||
describe('.bump()', function () { | ||
it('Should work with standard errors and a message', function () { | ||
var err = YError.bump(new Error('This is an error!')); | ||
assert.equal(err.code, 'E_UNEXPECTED'); | ||
@@ -151,13 +135,10 @@ assert.equal(err.wrappedErrors.length, 1); | ||
} | ||
assert.equal(err.name, err.toString()); | ||
}); | ||
it('Should work with YError like errors', function () { | ||
var baseErr = new Error('E_A_NEW_ERROR'); | ||
baseErr.code = 'E_A_NEW_ERROR'; | ||
baseErr.params = ['baseParam1', 'baseParam2']; | ||
var err = YError.bump(baseErr); | ||
assert.equal(err.code, 'E_A_NEW_ERROR'); | ||
@@ -171,8 +152,7 @@ assert.equal(err.wrappedErrors.length, 1); | ||
} | ||
assert.equal(err.name, err.toString()); | ||
}); | ||
it('Should work with Y errors and a message', function () { | ||
var err = YError.bump(new YError('E_ERROR', 'arg1.1', 'arg1.2'), 'E_ERROR_2', 'arg2.1', 'arg2.2'); | ||
assert.equal(err.code, 'E_ERROR'); | ||
@@ -185,2 +165,3 @@ assert.deepEqual(err.params, ['arg1.1', 'arg1.2']); | ||
} | ||
assert.equal(err.name, err.toString()); | ||
@@ -187,0 +168,0 @@ }); |
'use strict'; | ||
const { execSync } = require('child_process'); | ||
const SAUCE_TIMEOUT = 240000; | ||
@@ -7,7 +9,5 @@ | ||
const baseConfig = { | ||
// base path that will be used to resolve all patterns (eg. files, exclude) | ||
basePath: '', | ||
// frameworks to use | ||
@@ -17,18 +17,12 @@ // available frameworks: https://npmjs.org/browse/keyword/karma-adapter | ||
// list of files / patterns to load in the browser | ||
files: [ | ||
'src/**/*.js', | ||
], | ||
files: ['dist/**/*.js'], | ||
// list of files to exclude | ||
exclude: [ | ||
], | ||
exclude: [], | ||
// preprocess matching files before serving them to the browser | ||
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor | ||
preprocessors: { | ||
'src/**/*.js': ['browserify'], | ||
'dist/**/*.js': ['browserify'], | ||
}, | ||
@@ -40,19 +34,14 @@ | ||
// web server port | ||
port: 9876, | ||
// enable / disable colors in the output (reporters and logs) | ||
colors: true, | ||
// level of logging | ||
logLevel: config.LOG_INFO, | ||
// enable / disable watching file and executing tests whenever any file changes | ||
autoWatch: true, | ||
autoWatch: !process.env.SAUCE_USERNAME, | ||
// Continuous Integration mode | ||
@@ -67,13 +56,13 @@ // if true, Karma captures browsers, runs the tests and exits | ||
if( | ||
(!process.env.SAUCE_USERNAME) || | ||
( | ||
process.env.TRAVIS_PULL_REQUEST && | ||
'false' !== process.env.TRAVIS_PULL_REQUEST | ||
) | ||
if ( | ||
!process.env.SAUCE_USERNAME || | ||
!execSync('node -v') | ||
.toString() | ||
.trim() | ||
.startsWith('v10') || | ||
(process.env.TRAVIS_PULL_REQUEST && | ||
'false' !== process.env.TRAVIS_PULL_REQUEST) | ||
) { | ||
config.set(Object.assign( | ||
{}, | ||
baseConfig, | ||
{ | ||
config.set( | ||
Object.assign({}, baseConfig, { | ||
// test results reporter to use | ||
@@ -86,10 +75,7 @@ // possible values: 'dots', 'progress' | ||
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | ||
browsers: ['Firefox'] | ||
.concat( | ||
'true' === process.env.TRAVIS ? | ||
[] : | ||
['Chrome'] | ||
), | ||
} | ||
)); | ||
browsers: ['Firefox'].concat( | ||
'true' === process.env.TRAVIS ? [] : ['Chrome'], | ||
), | ||
}), | ||
); | ||
return; | ||
@@ -102,12 +88,13 @@ } | ||
base: 'SauceLabs', | ||
browserName: 'chrome', | ||
browserName: 'Chrome', | ||
platform: 'Windows 10', | ||
}, | ||
SL_Firefox: { | ||
base: 'SauceLabs', | ||
browserName: 'firefox', | ||
browserName: 'Firefox', | ||
platform: 'Windows 10', | ||
}, | ||
SL_IEEdge: { | ||
base: 'SauceLabs', | ||
browserName: 'microsoftedge', | ||
version: null, | ||
browserName: 'MicrosoftEdge', | ||
platform: 'Windows 10', | ||
@@ -117,12 +104,10 @@ }, | ||
config.set(Object.assign( | ||
{}, | ||
baseConfig, | ||
{ | ||
config.set( | ||
Object.assign({}, baseConfig, { | ||
reporters: ['dots', 'saucelabs'], | ||
customLaunchers: customLaunchers, | ||
customLaunchers, | ||
// Increase timeouts to prevent the issue with disconnected tests (https://goo.gl/nstA69) | ||
captureTimeout: SAUCE_TIMEOUT, | ||
browserDisconnectTimeout: 10000, | ||
browserDisconnectTimeout: SAUCE_TIMEOUT, | ||
browserDisconnectTolerance: 1, | ||
@@ -139,10 +124,12 @@ browserNoActivityTimeout: SAUCE_TIMEOUT, | ||
public: 'public', | ||
build: `build-${execSync('git rev-parse HEAD') | ||
.toString() | ||
.trim()}`, | ||
}, | ||
// start these browsers | ||
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | ||
browsers: Object.keys(customLaunchers), | ||
} | ||
)); | ||
}), | ||
); | ||
}; |
105
package.json
{ | ||
"name": "yerror", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "It helps to know why you got an error.", | ||
@@ -9,8 +9,10 @@ "main": "src/index.js", | ||
"scripts": { | ||
"build": "rm -rf dist && babel src -d dist && npm run test:build", | ||
"compile": "babel --extensions '.js' src --out-dir=dist", | ||
"build": "rm -rf dist && npm run compile && npm run test:build", | ||
"cli": "env NPM_RUN_CLI=1", | ||
"cover": "istanbul cover --report html _mocha -- src/*.mocha.js -R spec -t 5000", | ||
"coveralls": "istanbul cover _mocha --report lcovonly -- src/*.mocha.js -R spec -t 5000 && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", | ||
"cover": "nyc npm test && nyc report --reporter=html --reporter=text", | ||
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls && rm -rf ./coverage", | ||
"lint": "eslint src/*.js", | ||
"preversion": "npm run lint && npm build && npm test", | ||
"prettier": "prettier --write src/*.js", | ||
"preversion": "npm run lint && npm build && npm t", | ||
"test": "mocha src/*.mocha.js && karma start karma.conf.js", | ||
@@ -20,3 +22,3 @@ "test:build": "mocha dist/*.mocha.js" | ||
"engines": { | ||
"node": ">=6.0.0" | ||
"node": ">=8.12.0" | ||
}, | ||
@@ -32,3 +34,7 @@ "repository": { | ||
], | ||
"author": "Nicolas Froidure (Simplifield)", | ||
"author": { | ||
"name": "Nicolas Froidure (formerly at SimpliField)", | ||
"email": "nicolas.froidure@insertafter.com", | ||
"url": "http://insertafter.com/en/index.html" | ||
}, | ||
"license": "MIT", | ||
@@ -39,20 +45,75 @@ "bugs": { | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"@babel/cli": "^7.7.4", | ||
"@babel/core": "^7.7.4", | ||
"@babel/register": "^7.7.4", | ||
"@babel/preset-env": "^7.7.4", | ||
"@babel/plugin-proposal-class-properties": "^7.7.4", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.7.4", | ||
"@babel/plugin-transform-classes": "^7.7.4", | ||
"babel-eslint": "^10.0.3", | ||
"babel-plugin-transform-builtin-extend": "^1.1.2", | ||
"babel-preset-es2015": "^6.24.1", | ||
"browserify": "^14.4.0", | ||
"coveralls": "~2.11.2", | ||
"eslint": "^3.4.0", | ||
"eslint-config-simplifield": "^4.1.1", | ||
"istanbul": "^0.4.2", | ||
"karma": "^1.7.0", | ||
"karma-browserify": "^5.1.1", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-firefox-launcher": "^1.0.1", | ||
"browserify": "^16.5.0", | ||
"coveralls": "^3.0.9", | ||
"eslint": "^6.7.2", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"karma": "^4.4.1", | ||
"karma-browserify": "^6.1.0", | ||
"karma-chrome-launcher": "^3.1.0", | ||
"karma-firefox-launcher": "^1.2.0", | ||
"karma-mocha": "^1.3.0", | ||
"karma-sauce-launcher": "^1.1.0", | ||
"mocha": "^3.0.2", | ||
"mocha-lcov-reporter": "^1.0.0" | ||
"karma-sauce-launcher": "^2.0.2", | ||
"mocha": "^6.2.2", | ||
"nyc": "^14.1.1", | ||
"prettier": "^1.19.1" | ||
}, | ||
"dependencies": {} | ||
"dependencies": {}, | ||
"babel": { | ||
"presets": [ | ||
"@babel/env" | ||
], | ||
"plugins": [ | ||
[ | ||
"@babel/plugin-transform-classes", | ||
{} | ||
], | ||
[ | ||
"babel-plugin-transform-builtin-extend", | ||
{ | ||
"globals": [ | ||
"Error", | ||
"Array" | ||
] | ||
} | ||
] | ||
] | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"eslint:recommended" | ||
], | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "script", | ||
"modules": true | ||
}, | ||
"env": { | ||
"es6": true, | ||
"node": true, | ||
"jest": true, | ||
"mocha": true | ||
}, | ||
"plugins": [ | ||
"prettier" | ||
], | ||
"rules": { | ||
"prettier/prettier": "error" | ||
} | ||
}, | ||
"prettier": { | ||
"semi": true, | ||
"printWidth": 80, | ||
"singleQuote": true, | ||
"trailingComma": "all", | ||
"proseWrap": "always" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # YError | ||
[![NPM version](https://badge.fury.io/js/yerror.svg)](https://npmjs.org/package/yerror) [![Build status](https://secure.travis-ci.org/SimpliField/yerror.png)](https://travis-ci.org/SimpliField/yerror) [![Dependency Status](https://david-dm.org/SimpliField/yerror/status.svg)](https://david-dm.org/SimpliField/yerror) [![devDependency Status](https://david-dm.org/SimpliField/yerror/dev-status.svg)](https://david-dm.org/SimpliField/yerror#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/SimpliField/yerror/badge.svg?branch=master)](https://coveralls.io/r/SimpliField/yerror?branch=master) [![Code Climate](https://codeclimate.com/github/SimpliField/yerror.png)](https://codeclimate.com/github/SimpliField/yerror) | ||
[![NPM version](https://badge.fury.io/js/yerror.svg)](https://npmjs.org/package/yerror) [![Build status](https://secure.travis-ci.org/nfroidure/yerror.png)](https://travis-ci.org/nfroidure/yerror) [![Dependency Status](https://david-dm.org/nfroidure/yerror/status.svg)](https://david-dm.org/nfroidure/yerror) [![devDependency Status](https://david-dm.org/nfroidure/yerror/dev-status.svg)](https://david-dm.org/nfroidure/yerror#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/nfroidure/yerror/badge.svg?branch=master)](https://coveralls.io/r/nfroidure/yerror?branch=master) [![Code Climate](https://codeclimate.com/github/nfroidure/yerror.png)](https://codeclimate.com/github/nfroidure/yerror) | ||
@@ -29,4 +29,4 @@ ## Usage | ||
// YError: E_BAD_PAY (nuts, code) | ||
// at doSomething (/home/nfroidure/simplifield/yerror/test.js:5:11) | ||
// at Object.<anonymous> (/home/nfroidure/simplifield/yerror/test.js:9:1) | ||
// at doSomething (/home/nfroidure/nfroidure/yerror/test.js:5:11) | ||
// at Object.<anonymous> (/home/nfroidure/nfroidure/yerror/test.js:9:1) | ||
// (...) | ||
@@ -59,8 +59,8 @@ | ||
// YError: E_BAD_PAY (nuts, code) | ||
// at doSomething (/home/nfroidure/simplifield/yerror/test.js:5:11) | ||
// at doSomething (/home/nfroidure/nfroidure/yerror/test.js:5:11) | ||
// (...) | ||
// YError: E_BAD_TRANSACTION (pay) | ||
// at Function.YError.wrap (/home/nfroidure/simplifield/yerror/src/index.js:41:12) | ||
// at /home/nfroidure/simplifield/yerror/test.js:16:21 | ||
// at doSomethingAsync (/home/nfroidure/simplifield/yerror/test.js:11:11) | ||
// at Function.YError.wrap (/home/nfroidure/nfroidure/yerror/src/index.js:41:12) | ||
// at /home/nfroidure/nfroidure/yerror/test.js:16:21 | ||
// at doSomethingAsync (/home/nfroidure/nfroidure/yerror/test.js:11:11) | ||
// (...) | ||
@@ -67,0 +67,0 @@ |
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
const os = require('os'); | ||
@@ -9,8 +7,6 @@ | ||
// Detecting if wrappedErrors are passed | ||
if(!(wrappedErrors instanceof Array)) { | ||
params = ( | ||
'undefined' === typeof errorCode ? | ||
[] : | ||
[errorCode] | ||
).concat(params); | ||
if (!(wrappedErrors instanceof Array)) { | ||
params = ('undefined' === typeof errorCode ? [] : [errorCode]).concat( | ||
params, | ||
); | ||
errorCode = wrappedErrors; | ||
@@ -29,3 +25,3 @@ wrappedErrors = []; | ||
if(Error.captureStackTrace) { | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, this.constructor); | ||
@@ -37,9 +33,13 @@ } | ||
return ( | ||
this.wrappedErrors.length ? | ||
// eslint-disable-next-line | ||
this.wrappedErrors[this.wrappedErrors.length - 1].stack + os.EOL : | ||
'' | ||
) + | ||
this.constructor.name + ': ' + this.code + | ||
' (' + this.params.join(', ') + ')'; | ||
(this.wrappedErrors.length | ||
? // eslint-disable-next-line | ||
this.wrappedErrors[this.wrappedErrors.length - 1].stack + os.EOL | ||
: '') + | ||
this.constructor.name + | ||
': ' + | ||
this.code + | ||
' (' + | ||
this.params.join(', ') + | ||
')' | ||
); | ||
} | ||
@@ -54,4 +54,4 @@ } | ||
if(!errorCode) { | ||
if(wrappedErrorIsACode) { | ||
if (!errorCode) { | ||
if (wrappedErrorIsACode) { | ||
errorCode = err.message; | ||
@@ -62,3 +62,3 @@ } else { | ||
} | ||
if(err.message && !wrappedErrorIsACode) { | ||
if (err.message && !wrappedErrorIsACode) { | ||
params.push(err.message); | ||
@@ -71,3 +71,3 @@ } | ||
YError.cast = function yerrorCast(err, ...params) { | ||
if(_looksLikeAYError(err)) { | ||
if (_looksLikeAYError(err)) { | ||
return err; | ||
@@ -79,3 +79,3 @@ } | ||
YError.bump = function yerrorBump(err, ...params) { | ||
if(_looksLikeAYError(err)) { | ||
if (_looksLikeAYError(err)) { | ||
return YError.wrap.apply(YError, [err, err.code].concat(err.params)); | ||
@@ -89,8 +89,11 @@ } | ||
function _looksLikeAYError(err) { | ||
return err instanceof YError || ( | ||
err.constructor && | ||
err.constructor.name && | ||
err.constructor.name.endsWith('Error') && | ||
'string' === typeof err.code && _looksLikeAYErrorCode(err.code) && | ||
err.params && err.params instanceof Array | ||
return ( | ||
err instanceof YError || | ||
(err.constructor && | ||
err.constructor.name && | ||
err.constructor.name.endsWith('Error') && | ||
'string' === typeof err.code && | ||
_looksLikeAYErrorCode(err.code) && | ||
err.params && | ||
err.params instanceof Array) | ||
); | ||
@@ -100,5 +103,5 @@ } | ||
function _looksLikeAYErrorCode(str) { | ||
return (/^([A-Z0-9_]+)$/).test(str); | ||
return /^([A-Z0-9_]+)$/.test(str); | ||
} | ||
module.exports = YError; |
/* eslint max-nested-callbacks:[0], no-magic-numbers:[0] */ | ||
'use strict'; | ||
var assert = require('assert'); | ||
@@ -8,5 +6,3 @@ var YError = require('./index.js'); | ||
describe('YError', () => { | ||
describe('.__constructor', () => { | ||
it('Should work', () => { | ||
@@ -38,8 +34,7 @@ var err = new YError('E_ERROR', 'arg1', 'arg2'); | ||
}); | ||
}); | ||
describe('.wrap()', () => { | ||
it('Should work with standard errors and a message', () => { // eslint-disable-line | ||
it('Should work with standard errors and a message', () => { | ||
// eslint-disable-line | ||
var err = YError.wrap(new Error('This is an error!')); | ||
@@ -51,10 +46,10 @@ | ||
if(Error.captureStackTrace) { | ||
if (Error.captureStackTrace) { | ||
assert( | ||
-1 !== err.stack.indexOf('Error: This is an error!'), | ||
'Stack contains original error.' | ||
'Stack contains original error.', | ||
); | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_UNEXPECTED (This is an error!)'), | ||
'Stack contains cast error.' | ||
'Stack contains cast error.', | ||
); | ||
@@ -72,10 +67,10 @@ assert.equal(err.name, err.toString()); | ||
if(Error.captureStackTrace) { | ||
if (Error.captureStackTrace) { | ||
assert( | ||
-1 !== err.stack.indexOf('Error: E_ERROR'), | ||
'Stack contains original error.' | ||
'Stack contains original error.', | ||
); | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_ERROR ()'), | ||
'Stack contains cast error.' | ||
'Stack contains cast error.', | ||
); | ||
@@ -93,10 +88,10 @@ } | ||
if(Error.captureStackTrace) { | ||
if (Error.captureStackTrace) { | ||
assert( | ||
-1 !== err.stack.indexOf('Error: E_ERROR'), | ||
'Stack contains first error.' | ||
'Stack contains first error.', | ||
); | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_ERROR_2 (arg1, arg2)'), | ||
'Stack contains second error.' | ||
'Stack contains second error.', | ||
); | ||
@@ -109,11 +104,6 @@ } | ||
var err = YError.wrap( | ||
YError.wrap( | ||
new Error('E_ERROR_1'), | ||
'E_ERROR_2', | ||
'arg2.1', | ||
'arg2.2' | ||
), | ||
YError.wrap(new Error('E_ERROR_1'), 'E_ERROR_2', 'arg2.1', 'arg2.2'), | ||
'E_ERROR_3', | ||
'arg3.1', | ||
'arg3.2' | ||
'arg3.2', | ||
); | ||
@@ -125,14 +115,14 @@ | ||
if(Error.captureStackTrace) { | ||
if (Error.captureStackTrace) { | ||
assert( | ||
-1 !== err.stack.indexOf('Error: E_ERROR_1'), | ||
'Stack contains first error.' | ||
'Stack contains first error.', | ||
); | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_ERROR_2 (arg2.1, arg2.2)'), | ||
'Stack contains second error.' | ||
'Stack contains second error.', | ||
); | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_ERROR_3 (arg3.1, arg3.2)'), | ||
'Stack contains second error.' | ||
'Stack contains second error.', | ||
); | ||
@@ -142,7 +132,5 @@ } | ||
}); | ||
}); | ||
describe('.cast()', () => { | ||
it('Should work with standard errors and a message', () => { | ||
@@ -155,10 +143,10 @@ var err = YError.cast(new Error('This is an error!')); | ||
if(Error.captureStackTrace) { | ||
if (Error.captureStackTrace) { | ||
assert( | ||
-1 !== err.stack.indexOf('Error: This is an error!'), | ||
'Stack contains original error.' | ||
'Stack contains original error.', | ||
); | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_UNEXPECTED (This is an error!)'), | ||
'Stack contains cast error.' | ||
'Stack contains cast error.', | ||
); | ||
@@ -175,6 +163,6 @@ } | ||
if(Error.captureStackTrace) { | ||
if (Error.captureStackTrace) { | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_ERROR (arg1, arg2)'), | ||
'Stack contains cast error.' | ||
'Stack contains cast error.', | ||
); | ||
@@ -184,7 +172,5 @@ } | ||
}); | ||
}); | ||
describe('.bump()', () => { | ||
it('Should work with standard errors and a message', () => { | ||
@@ -197,10 +183,10 @@ var err = YError.bump(new Error('This is an error!')); | ||
if(Error.captureStackTrace) { | ||
if (Error.captureStackTrace) { | ||
assert( | ||
-1 !== err.stack.indexOf('Error: This is an error!'), | ||
'Stack contains original error.' | ||
'Stack contains original error.', | ||
); | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_UNEXPECTED (This is an error!)'), | ||
'Stack contains bumped error.' | ||
'Stack contains bumped error.', | ||
); | ||
@@ -223,10 +209,11 @@ } | ||
if(Error.captureStackTrace) { | ||
if (Error.captureStackTrace) { | ||
assert( | ||
-1 !== err.stack.indexOf('Error: E_A_NEW_ERROR'), | ||
'Stack contains original error.' | ||
'Stack contains original error.', | ||
); | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_A_NEW_ERROR (baseParam1, baseParam2)'), | ||
'Stack contains bumped error.' | ||
-1 !== | ||
err.stack.indexOf('YError: E_A_NEW_ERROR (baseParam1, baseParam2)'), | ||
'Stack contains bumped error.', | ||
); | ||
@@ -240,3 +227,5 @@ } | ||
new YError('E_ERROR', 'arg1.1', 'arg1.2'), | ||
'E_ERROR_2', 'arg2.1', 'arg2.2' | ||
'E_ERROR_2', | ||
'arg2.1', | ||
'arg2.2', | ||
); | ||
@@ -247,10 +236,10 @@ | ||
if(Error.captureStackTrace) { | ||
if (Error.captureStackTrace) { | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_ERROR (arg1.1, arg1.2)'), | ||
'Stack contains original error.' | ||
'Stack contains original error.', | ||
); | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_ERROR (arg1.1, arg1.2)'), | ||
'Stack contains bumped error.' | ||
'Stack contains bumped error.', | ||
); | ||
@@ -260,5 +249,3 @@ } | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances 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
49
115832
22
655
5
1