bugsy
Advanced tools
Comparing version 0.1.0 to 1.0.0
207
lib/index.js
@@ -1,99 +0,146 @@ | ||
"use strict"; | ||
'use strict'; | ||
var util = require('util'); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
module.exports = {}; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var LEVELS_SYSLOG = { | ||
EMERGENCY: 'emerg', | ||
ALERT: 'alert', | ||
CRITICAL: 'crit', | ||
ERROR: 'err', | ||
WARNING: 'warning', | ||
NOTICE: 'notice', | ||
INFORMATIONAL: 'info', | ||
DEBUG: 'debug' | ||
}; | ||
module.exports.LEVELS_SYSLOG = LEVELS_SYSLOG; | ||
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; }; }(); | ||
var LEVELS_SYSLOG_ORDER = {}; | ||
LEVELS_SYSLOG_ORDER[LEVELS_SYSLOG.EMERGENCY] = 0; | ||
LEVELS_SYSLOG_ORDER[LEVELS_SYSLOG.ALERT] = 1; | ||
LEVELS_SYSLOG_ORDER[LEVELS_SYSLOG.CRITICAL] = 2; | ||
LEVELS_SYSLOG_ORDER[LEVELS_SYSLOG.ERROR] = 3; | ||
LEVELS_SYSLOG_ORDER[LEVELS_SYSLOG.WARNING] = 4; | ||
LEVELS_SYSLOG_ORDER[LEVELS_SYSLOG.NOTICE] = 5; | ||
LEVELS_SYSLOG_ORDER[LEVELS_SYSLOG.INFORMATIONAL] = 6; | ||
LEVELS_SYSLOG_ORDER[LEVELS_SYSLOG.DEBUG] = 7; | ||
module.exports.LEVELS_SYSLOG_ORDER = LEVELS_SYSLOG_ORDER; | ||
exports.toString = toString; | ||
exports.newError = newError; | ||
function create(name, message, options) { | ||
var finalOptions = options || {}; | ||
var defaultCode = finalOptions.code; | ||
var defaultLevel = finalOptions.level; | ||
var inherits = finalOptions.inherits || Error; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var CustomError = function(options) { | ||
CustomError.super_.apply(this, arguments); | ||
var finalOptions = options || {}; | ||
this.name = name; | ||
this.message = message; | ||
this.level = finalOptions.level || defaultLevel; | ||
this.code = finalOptions.code || defaultCode; | ||
this.meta = finalOptions.meta; | ||
Error.captureStackTrace(this, this.constructor); | ||
}; | ||
util.inherits(CustomError, inherits); | ||
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; } | ||
return CustomError; | ||
} | ||
module.exports.create = create; | ||
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 AggregateError() {}; | ||
util.inherits(AggregateError, Array); | ||
module.exports.AggregateError = AggregateError; | ||
function _extendableBuiltin(cls) { | ||
function ExtendableBuiltin() { | ||
cls.apply(this, arguments); | ||
} | ||
function composeReducers() { | ||
var reducers = arguments; | ||
return function(error) { | ||
var length = reducers.length; | ||
for (var i = 0; i < length; i++) { | ||
var status = reducers[i](error); | ||
if (status !== undefined) { | ||
return status; | ||
} | ||
ExtendableBuiltin.prototype = Object.create(cls.prototype, { | ||
constructor: { | ||
value: cls, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
} | ||
} | ||
module.exports.composeReducers = composeReducers; | ||
}); | ||
function reduce(reducers, fn) { | ||
var composed = composeReducers.apply(this, reducers); | ||
return function() { | ||
return fn.apply(this, arguments).catch(function (error) { | ||
error.status = composed(error); | ||
throw error; | ||
}); | ||
if (Object.setPrototypeOf) { | ||
Object.setPrototypeOf(ExtendableBuiltin, cls); | ||
} else { | ||
ExtendableBuiltin.__proto__ = cls; | ||
} | ||
} | ||
module.exports.reduce = reduce; | ||
function withCode(error, code) { | ||
return error.code === code; | ||
return ExtendableBuiltin; | ||
} | ||
module.exports.withCode = withCode; | ||
function everyWithCode(error, code) { | ||
return util.isArray(error) && error.every(function(error) { return error.code === code; }); | ||
var SYSLOG_EMERGENCY = exports.SYSLOG_EMERGENCY = 'emerg'; | ||
var SYSLOG_ALERT = exports.SYSLOG_ALERT = 'alert'; | ||
var SYSLOG_CRITICAL = exports.SYSLOG_CRITICAL = 'crit'; | ||
var SYSLOG_ERROR = exports.SYSLOG_ERROR = 'err'; | ||
var SYSLOG_WARNING = exports.SYSLOG_WARNING = 'warning'; | ||
var SYSLOG_NOTICE = exports.SYSLOG_NOTICE = 'notice'; | ||
var SYSLOG_INFORMATIONAL = exports.SYSLOG_INFORMATIONAL = 'info'; | ||
var SYSLOG_DEBUG = exports.SYSLOG_DEBUG = 'debug'; | ||
function transform(name) { | ||
var words = name.split(/[\s_-]/); | ||
return words.map(function (word) { | ||
return word[0].toUpperCase() + word.slice(1).toLowerCase(); | ||
}).join(''); | ||
} | ||
module.exports.everyWithCode = everyWithCode; | ||
function instanceOf(error, type) { | ||
return error instanceof type; | ||
function toString(err) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var _ref$withDate = _ref.withDate; | ||
var withDate = _ref$withDate === undefined ? true : _ref$withDate; | ||
var severity = err.severity || SYSLOG_ERROR; | ||
var message = err.message || ''; | ||
var code = err.code || err.name || 'Error'; | ||
var buf = ''; | ||
if (withDate) { | ||
buf += new Date().toISOString() + ' '; | ||
} | ||
buf += '[' + severity + '] '; | ||
buf += code + ' '; | ||
if (message) { | ||
buf += message; | ||
} | ||
return buf; | ||
} | ||
module.exports.instanceOf = instanceOf; | ||
function everyInstanceOf(error, type) { | ||
return util.isArray(error) && error.every(function(error) { return error instanceof type; }); | ||
} | ||
module.exports.everyInstanceOf = everyInstanceOf; | ||
var ExtendableError = exports.ExtendableError = function (_extendableBuiltin2) { | ||
_inherits(ExtendableError, _extendableBuiltin2); | ||
function ExtendableError(code, message) { | ||
var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var _ref2$severity = _ref2.severity; | ||
var severity = _ref2$severity === undefined ? SYSLOG_ERROR : _ref2$severity; | ||
_classCallCheck(this, ExtendableError); | ||
var _this = _possibleConstructorReturn(this, (ExtendableError.__proto__ || Object.getPrototypeOf(ExtendableError)).call(this)); | ||
_this.message = message; | ||
_this.name = transform(code) + 'Error'; | ||
_this.code = code; | ||
_this.severity = severity; | ||
_this.meta = {}; | ||
var error = new Error(message); | ||
error.name = _this.name; | ||
var stack = error.stack.split('\n'); | ||
stack.splice(1, 2); | ||
_this.stack = stack.join('\n'); | ||
return _this; | ||
} | ||
_createClass(ExtendableError, [{ | ||
key: 'addSeverity', | ||
value: function addSeverity(value) { | ||
this.severity = value; | ||
return this; | ||
} | ||
}, { | ||
key: 'addMeta', | ||
value: function addMeta(value) { | ||
this.meta = _extends({}, this.meta, value); | ||
return this; | ||
} | ||
// eslint-disable-next-line class-methods-use-this | ||
}, { | ||
key: 'marshal', | ||
value: function marshal() { | ||
return {}; | ||
} | ||
}]); | ||
return ExtendableError; | ||
}(_extendableBuiltin(Error)); | ||
function newError(code, message) { | ||
return function (_ExtendableError) { | ||
_inherits(_class, _ExtendableError); | ||
function _class(options) { | ||
_classCallCheck(this, _class); | ||
return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this, code, message, options)); | ||
} | ||
return _class; | ||
}(ExtendableError); | ||
} |
{ | ||
"name": "bugsy", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Collection of helpers to deal with errors in Node.js", | ||
@@ -30,10 +30,33 @@ "author": "Nicolas Jakob <nicow.jakob@gmail.com>", | ||
"scripts": { | ||
"test": "make test" | ||
"clean": "rimraf lib", | ||
"compile": "babel src -d lib --ignore 'tests/*.js' && flow gen-flow-files --out-dir lib src/index.js", | ||
"lint": "eslint --fix .", | ||
"flow": "flow check", | ||
"test": "npm run lint && npm run flow && npm run compile && mocha --compilers js:babel-core/register 'src/**/tests/**/*.js'", | ||
"bump-major": "npm version major -m \"Bump to %s\"", | ||
"bump-minor": "npm version minor -m \"Bump to %s\"", | ||
"bump-patch": "npm version patch -m \"Bump to %s\"", | ||
"prepublish": "npm run test" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.16.0", | ||
"babel-core": "^6.17.0", | ||
"babel-eslint": "^6.1.2", | ||
"babel-plugin-syntax-flow": "^6.13.0", | ||
"babel-plugin-transform-builtin-extend": "^1.1.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.14.0", | ||
"babel-preset-es2015": "^6.14.0", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"chai": "^3.5.0", | ||
"eslint": "^3.5.0", | ||
"eslint-config-njakob": "^2.1.0", | ||
"eslint-plugin-flowtype": "^2.19.0", | ||
"eslint-plugin-import": "^1.15.0", | ||
"flow-bin": "^0.33.0", | ||
"istanbul": "0.4.4", | ||
"koa": "2.0.0", | ||
"mocha": "2.5.3", | ||
"rimraf": "^2.5.4", | ||
"should": "10.0.0" | ||
} | ||
} |
# bugsy | ||
[![NPM version][npm-status-image]][npm] | ||
Collection of helpers to deal with errors. | ||
@@ -13,9 +15,9 @@ | ||
* Inheritance management | ||
* Isomorphic module | ||
* Error severity support | ||
* Flowtype definition | ||
* Flowtype support | ||
## Installation | ||
[![NPM](https://nodei.co/npm/bugsy.png?downloads=true)](https://nodei.co/npm/bugsy/) | ||
[![NPM][npm-install-image]][npm] | ||
@@ -31,32 +33,38 @@ ``` | ||
const RanAwayError = bugsy.create( | ||
'RanAwayError', | ||
'It ran away, again', | ||
{ code: 'ran_away' } | ||
); | ||
const MissedThrowError = bugsy.create( | ||
'MissedThrowError', | ||
'Throw totally missed', | ||
{ code: 'missed_throw' } | ||
); | ||
const CODE_RAN_AWAY = 'ran_away'; | ||
const CODE_THROW_MISSED = 'throw_missed'; | ||
const RanAwayError = bugsy.newError(CODE_RAN_AWAY, 'It ran away, again'); | ||
const ThrowMissedError = bugsy.newError(CODE_THROW_MISSED, 'Throw totally missed'); | ||
function capture() { | ||
if (Math.random() > 0.9) { | ||
throw new MissedThrowError(); | ||
throw new ThrowMissedError({ severity: bugsy.SYSLOG_WARNING }); | ||
} else { | ||
throw new RanAwayError({ meta: 'Caterpie' }); | ||
throw new RanAwayError(); | ||
} | ||
} | ||
try { | ||
capture(); | ||
} catch (error) { | ||
switch (true) { | ||
case bugsy.withCode(error, 'missed_throw'): | ||
break; | ||
case bugsy.instanceOf(error, RanAwayError): | ||
error.level = bugsy.LEVELS_SYSLOG.EMERGENCY; | ||
throw error; | ||
function handler(fn) { | ||
try { | ||
fn(); | ||
} catch (err) { | ||
console.log(bugsy.toString(err)); | ||
} | ||
} | ||
handler(() => { | ||
try { | ||
capture(); | ||
} catch (err) { | ||
switch (true) { | ||
case err.code === CODE_THROW_MISSED: | ||
break; | ||
case err instanceof RanAwayError: | ||
throw (err.addSeverity(bugsy.SYSLOG_EMERGENCY)); | ||
default: | ||
throw err; | ||
} | ||
} | ||
}); | ||
``` | ||
@@ -66,2 +74,7 @@ | ||
[MIT](LICENSE) | ||
Bugsy is licensed under the [MIT License][licence]. | ||
[licence]: LICENSE | ||
[npm]: https://nodei.co/npm/bugsy/ | ||
[npm-install-image]: https://nodei.co/npm/bugsy.png?downloads=true | ||
[npm-status-image]: https://img.shields.io/npm/v/bugsy.svg |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10855
114
0
77
19
1