Comparing version 0.4.1 to 1.0.0
231
eraro.js
@@ -1,25 +0,20 @@ | ||
/* Copyright (c) 2014-2015 Richard Rodger, MIT License */ | ||
/* Copyright (c) 2014-2018 Richard Rodger, MIT License */ | ||
/* jshint node:true, asi:true, eqnull:true */ | ||
// Create JavaScript Error objects with code strings, context details, | ||
// and templated messages. | ||
"use strict"; | ||
'use strict' | ||
// #### System modules | ||
var util = require('util') | ||
// #### External modules | ||
var _ = require('lodash') | ||
// #### Exports | ||
module.exports = eraro | ||
// #### Create an _eraro_ function | ||
// Parameters: | ||
// | ||
// | ||
// * _options_ : (optional) Object; properties: | ||
@@ -29,3 +24,3 @@ // * _package_ : (optional) String; package name to mark Error objects | ||
// * _module_ : (optional) Object; _module_ object to use as starting point for _require_ calls | ||
// * _msgmap_ : (optional) Object; map codes to message templates | ||
// * _msgmap_ : (optional) Object; map codes to message templates | ||
// * _inspect_ : (optional) Boolean; If true, _util.inspect_ is called on values; default: true. | ||
@@ -36,3 +31,3 @@ // | ||
// The created function has parameters: | ||
// | ||
// | ||
// * _exception_ : (optional) Error; the original exception to be wrapped | ||
@@ -45,3 +40,3 @@ // * _code_ : (optional) String; code value | ||
// The returned Error object has the following additional properties: | ||
// | ||
// | ||
// * _code_: String; the code string | ||
@@ -52,55 +47,75 @@ // * _package_: String; the package name | ||
// * _details_: Object; contextual details of error | ||
// * _callpoint_: String; first line of stacktrace that is external to eraro and calling module | ||
function eraro( options ) { | ||
// * _callpoint_: String; first line of stacktrace that is external to eraro and calling module | ||
function eraro(options) { | ||
options = options || {} | ||
var msgprefix = false === options.prefix ? '' : | ||
(_.isString(options.prefix) ? options.prefix : _.isString(options.package) ? options.package+': ' : '') | ||
var msgprefix = | ||
false === options.prefix | ||
? '' | ||
: _.isString(options.prefix) | ||
? options.prefix | ||
: _.isString(options.package) | ||
? options.package + ': ' | ||
: '' | ||
var packaje = options.package || 'unknown' | ||
var callmodule = options.module || module | ||
var msgmap = options.msgmap || {} | ||
var inspect = null == options.inspect ? true : !!options.inspect | ||
var packaje = options.package || 'unknown' | ||
var callmodule = options.module || module | ||
var msgmap = options.msgmap || {} | ||
var inspect = null == options.inspect ? true : !!options.inspect | ||
var markers = [module.filename] | ||
var markers = [module.filename] | ||
var filename = callmodule.filename | ||
if( filename ) markers.push(filename); | ||
if (filename) markers.push(filename) | ||
var errormaker = function( ex, code, msg, details ) { | ||
var errormaker = function(ex, code, msg, details) { | ||
var internalex = false | ||
if( util.isError(ex) ) { | ||
if( ex.eraro && !options.override ) return ex; | ||
} | ||
else { | ||
if (util.isError(ex)) { | ||
if (ex.eraro && !options.override) return ex | ||
} else { | ||
internalex = true | ||
ex = null | ||
code = arguments[0] | ||
msg = arguments[1] | ||
details = arguments[2] | ||
ex = null | ||
code = arguments[0] | ||
msg = arguments[1] | ||
details = arguments[2] | ||
} | ||
code = _.isString(code) ? code : | ||
(ex ? | ||
ex.code ? ex.code : | ||
ex.message ? ex.message : | ||
'unknown' : 'unknown') | ||
code = _.isString(code) | ||
? code | ||
: ex | ||
? ex.code | ||
? ex.code | ||
: ex.message | ||
? ex.message | ||
: 'unknown' | ||
: 'unknown' | ||
details = _.isObject(details) ? details : | ||
(_.isObject(msg) && !_.isString(msg) ? msg : {}) | ||
details = _.isObject(details) | ||
? details | ||
: _.isObject(msg) && !_.isString(msg) | ||
? msg | ||
: {} | ||
msg = _.isString(msg) ? msg : null | ||
msg = buildmessage(options,msg,msgmap,msgprefix,inspect,code,details,ex) | ||
msg = buildmessage( | ||
options, | ||
msg, | ||
msgmap, | ||
msgprefix, | ||
inspect, | ||
code, | ||
details, | ||
ex | ||
) | ||
var err = new Error(msg) | ||
if( ex ) { | ||
details.orig$ = null == details.orig$ ? ex : details.orig$ | ||
details.message$ = null == details.message$ ? ex.message : details.message$ | ||
if (ex) { | ||
details.orig$ = null == details.orig$ ? ex : details.orig$ | ||
details.message$ = | ||
null == details.message$ ? ex.message : details.message$ | ||
// drag along properties from original exception | ||
for( var p in ex ) { | ||
for (var p in ex) { | ||
err[p] = ex[p] | ||
@@ -110,15 +125,15 @@ } | ||
err.eraro = true | ||
err.eraro = true | ||
err.orig = ex // orig | ||
err.code = code | ||
err[packaje] = true | ||
err.package = packaje | ||
err.msg = msg | ||
err.details = details | ||
err.orig = ex // orig | ||
err.code = code | ||
err[packaje] = true | ||
err.package = packaje | ||
err.msg = msg | ||
err.details = details | ||
err.stack = ex ? ex.stack : err.stack | ||
err.callpoint = callpoint( err , markers ) | ||
err.stack = ex ? ex.stack : err.stack | ||
err.callpoint = callpoint(err, markers) | ||
return err; | ||
return err | ||
} | ||
@@ -128,10 +143,8 @@ | ||
return errormaker; | ||
return errormaker | ||
} | ||
// #### Find the first external stack trace line. | ||
// Parameters: | ||
// | ||
// | ||
// * _error_ : (optional) Error; provides the stack | ||
@@ -141,26 +154,25 @@ // * _markers_ : (optional) Array[String]; ignore lines containing these strings | ||
// Returns: String; stack trace line, with indent removed | ||
function callpoint( error, markers ) { | ||
function callpoint(error, markers) { | ||
markers = _.isArray(markers) ? markers : [] | ||
var stack = error ? error.stack : null | ||
var out = '' | ||
var out = '' | ||
if( stack ) { | ||
if (stack) { | ||
var lines = stack.split('\n') | ||
var done = false | ||
var done = false | ||
var i | ||
line_loop: | ||
for( i = 1; i < lines.length; i++ ) { | ||
line_loop: for (i = 1; i < lines.length; i++) { | ||
var line = lines[i] | ||
var found = false | ||
for( var j = 0; j < markers.length; j++ ) { | ||
if( _.isString( markers[j] ) ) { | ||
found = ( -1 != line.indexOf( markers[j] ) ) | ||
if( found ) break; | ||
for (var j = 0; j < markers.length; j++) { | ||
if (_.isString(markers[j])) { | ||
found = -1 != line.indexOf(markers[j]) | ||
if (found) break | ||
} | ||
} | ||
if( !found ) break line_loop; | ||
if (!found) break line_loop | ||
} | ||
@@ -174,6 +186,4 @@ | ||
// #### Build the message string from a template by inserting details | ||
// Uses the underscore template function with default settings. | ||
// Uses the underscore template function with default settings. | ||
// The original message (_msg_) has priority over messages from the _msgmap_. | ||
@@ -185,3 +195,3 @@ // If no message can be found, the _code_ is used as a message. | ||
// Parameters: | ||
// | ||
// | ||
// * _msg_ : (required) String; message template | ||
@@ -194,9 +204,24 @@ // * _msgmap_ : (required) Object; map codes to message templates | ||
// Returns: String; human readable error message | ||
function buildmessage(options,msg,msgmap,msgprefix,inspect,code,details,ex) { | ||
var message = msgprefix + (_.isString(msg) ? msg : | ||
_.isString(msgmap[code]) ? msgmap[code] : | ||
ex ? originalmsg(options.override,ex) : code ) | ||
function buildmessage( | ||
options, | ||
msg, | ||
msgmap, | ||
msgprefix, | ||
inspect, | ||
code, | ||
details, | ||
ex | ||
) { | ||
var message = | ||
msgprefix + | ||
(_.isString(msg) | ||
? msg | ||
: _.isString(msgmap[code]) | ||
? msgmap[code] | ||
: ex | ||
? originalmsg(options.override, ex) | ||
: code) | ||
// These are the inserts. | ||
var valmap = _.extend({},details,{code:code}) | ||
var valmap = _.extend({}, details, { code: code }) | ||
@@ -207,7 +232,13 @@ // Workaround to prevent underscore blowing up if properties are not | ||
var valstrmap = {util:util,_:_} | ||
_.each(valmap,function(val,key){ | ||
var valstrmap = { util: util, _: _ } | ||
_.each(valmap, function(val, key) { | ||
/* jshint evil:true */ | ||
try { eval('var '+key+';') } catch(e) { key = key+'$' } | ||
if( {'undefined':1,'NaN':1}[key] ) { key = key+'$' } | ||
try { | ||
eval('var ' + key + ';') | ||
} catch (e) { | ||
key = key + '$' | ||
} | ||
if ({ undefined: 1, NaN: 1 }[key]) { | ||
key = key + '$' | ||
} | ||
valstrmap[key] = inspect && !_.isString(val) ? util.inspect(val) : val | ||
@@ -217,15 +248,13 @@ }) | ||
var done = false | ||
while( !done ) { | ||
while (!done) { | ||
try { | ||
var tm = _.template( message ) | ||
var tm = _.template(message) | ||
message = tm(valstrmap) | ||
done = true | ||
} | ||
catch(e) { | ||
if(e instanceof ReferenceError) { | ||
} catch (e) { | ||
if (e instanceof ReferenceError) { | ||
var m = /ReferenceError:\s+(.*?)\s+/.exec(e.toString()) | ||
if( m && m[1] ) { | ||
valstrmap[m[1]]="["+m[1]+"?]" | ||
} | ||
else done = true | ||
if (m && m[1]) { | ||
valstrmap[m[1]] = '[' + m[1] + '?]' | ||
} else done = true | ||
} | ||
@@ -237,4 +266,8 @@ | ||
done = true | ||
message = message+' VALUES:'+util.inspect(valmap,{depth:2})+ | ||
' TEMPLATE ERROR: '+e | ||
message = | ||
message + | ||
' VALUES:' + | ||
util.inspect(valmap, { depth: 2 }) + | ||
' TEMPLATE ERROR: ' + | ||
e | ||
} | ||
@@ -247,10 +280,8 @@ } | ||
function originalmsg(override, ex) { | ||
if (!ex) return | ||
if (override && ex.eraro && ex.orig) return ex.orig.message | ||
function originalmsg( override, ex ) { | ||
if( !ex ) return; | ||
if( override && ex.eraro && ex.orig ) return ex.orig.message; | ||
return ex.message; | ||
return ex.message | ||
} |
{ | ||
"name": "eraro", | ||
"version": "0.4.1", | ||
"version": "1.0.0", | ||
"description": "Create JavaScript Error objects with code strings, context details, and templated messages.", | ||
"main": "eraro.js", | ||
"scripts": { | ||
"test": "./test.sh", | ||
"build": "./build.sh" | ||
"test": "lab -v -P test -t 80 -I URL,URLSearchParams", | ||
"coverage": "lab -s -P test -r lcov -I URL,URLSearchParams | coveralls", | ||
"prettier": "prettier --write --no-semi --single-quote *.js lib/*.js test/*.js" | ||
}, | ||
@@ -21,2 +22,5 @@ "repository": { | ||
"license": "MIT", | ||
"contributors": [ | ||
"Adrien Becchis (https://github.com/AdrieanKhisbe)" | ||
], | ||
"bugs": { | ||
@@ -27,3 +31,3 @@ "url": "https://github.com/rjrodger/eraro/issues" | ||
"dependencies": { | ||
"lodash": "~2.4.1" | ||
"lodash": "4.17" | ||
}, | ||
@@ -34,3 +38,9 @@ "files": [ | ||
"eraro.js" | ||
] | ||
], | ||
"devDependencies": { | ||
"code": "4", | ||
"coveralls": "3", | ||
"lab": "14", | ||
"prettier": "1" | ||
} | ||
} |
eraro | ||
===== | ||
[![npm version][npm-badge]][npm-url] | ||
[![Build Status][travis-badge]][travis-url] | ||
[![Coverage Status][coveralls-badge]][coveralls-url] | ||
[![Dependency Status][david-badge]][david-url] | ||
#### Create JavaScript Error objects with code strings, context details, and templated messages. | ||
Current Version: 0.4.1 | ||
Tested on: node 0.10.35 | ||
[![Build Status](https://travis-ci.org/rjrodger/eraro.png?branch=master)](https://travis-ci.org/rjrodger/eraro) | ||
[Annotated Source](http://rjrodger.github.io/eraro/doc/eraro.html) | ||
For use in library modules to generate contextual errors with useful | ||
@@ -20,13 +17,5 @@ meta data. Your library module can throw or pass (to a callback) an | ||
See the [use-plugin](http://github.com/rjrodger/use-plugin) module for | ||
an example of practical usage. | ||
# Support | ||
If you're using this module, feel free to contact me on twitter if you have any questions! :) [@rjrodger](http://twitter.com/rjrodger) | ||
[![Gitter chat](https://badges.gitter.im/rjrodger/eraro.png)](https://gitter.im/rjrodger/eraro) | ||
# Quick example | ||
@@ -52,4 +41,4 @@ | ||
In all these cases, the Error object will have a _code_ property with | ||
value _"code_string"_. | ||
In all these cases, the Error object will have a `code`` property with | ||
value `"code_string"`. | ||
@@ -79,4 +68,4 @@ | ||
The _error_ function can then be used in your library code. The | ||
_error_ function generates _Error_ objects, which can be thrown or used in callbacks: | ||
The `error` function can then be used in your library code. The | ||
`error` function generates `Error` objects, which can be thrown or used in callbacks: | ||
@@ -86,10 +75,10 @@ ```JavaScript | ||
function doStuff( input, callback ) { | ||
if( bad( input ) ) return callback( error('code2') ); | ||
function doStuff (input, callback) { | ||
if (bad(input)) return callback(error('code2')); | ||
} | ||
``` | ||
The _package_ option is normally the name of your library. That is, the value | ||
of the _name_ property in _package.json_. The generated Error object will | ||
have two properties to define the package: _package_, a string that is | ||
The `package` option is normally the name of your library. That is, the value | ||
of the `name` property in `package.json`. The generated Error object will | ||
have two properties to define the package: `package`, a string that is | ||
the name of the package, and also a boolean, the name of the package itself. | ||
@@ -116,3 +105,3 @@ This lets you check for the type of error easily: | ||
var err0 = error('code0',{foo:'FOO',bar:'BAR'}) | ||
var err0 = error('code0', {foo: 'FOO', bar: 'BAR'}) | ||
"FOO" === err0.details.foo | ||
@@ -123,3 +112,2 @@ "BAR" === err0.details.bar | ||
## Error codes and message templates | ||
@@ -130,3 +118,3 @@ | ||
```JavaScript | ||
var error = require('eraro')({package:'mylib',msgmap:{ | ||
var error = require('eraro')({package: 'mylib', msgmap: { | ||
code0: "The first error, foo is <%=foo%>.", | ||
@@ -140,7 +128,7 @@ code1: "The second error, bar is <%=bar%>.", | ||
```JavaScript | ||
var err0 = error('code0',{foo:'FOO',bar:'BAR'}) | ||
var err0 = error('code0',{foo: 'FOO', bar: 'BAR'}) | ||
"mylib: The first error, foo is FOO." === err0.message | ||
``` | ||
The message templates are [underscorejs templates](http://underscorejs.org/#template) | ||
The message templates are [underscorejs templates](http://underscorejs.org/#template) | ||
with the default settings. | ||
@@ -152,4 +140,4 @@ | ||
var err0 = error('code2', | ||
'My custom message, details: <%=util.inspect(zed)%>', | ||
{zed:{a:1,b:2}}) | ||
'My custom message, details: <%=util.inspect(zed)%>', | ||
{zed: {a: 1, b: 2}}) | ||
"mylib: My custom message, details: { a: 1, b: 2 }" === err0.message | ||
@@ -163,8 +151,8 @@ ``` | ||
* _code_: String; the code string | ||
* _package_: String; the package name | ||
* _**package-name**_: Boolean (true); a convenience marker for the package | ||
* _msg_: String; the generated message, may differ from original exception message (if any) | ||
* _details_: Object; contextual details of error | ||
* _callpoint_: String; first line of stacktrace that is external to eraro and calling module | ||
* `code`: String; the code string | ||
* `package`: String; the package name | ||
* **`package-name`**: Boolean (true); a convenience marker for the package | ||
* `msg`: String; the generated message, may differ from original exception message (if any) | ||
* `details`: Object; contextual details of error | ||
* `callpoint`: String; first line of stacktrace that is external to eraro and calling module | ||
@@ -178,9 +166,9 @@ You can pass in an existing Error object. The additional properties | ||
When creating an _error_ function, you can use the following options: | ||
When creating an `error` function, you can use the following options: | ||
* _package_ : (optional) String; package name to mark Error objects | ||
* _prefix_ : (optional) Boolean/String; If false, then no prefix is used; If not defined, the package name is used | ||
* _module_ : (optional) Object; _module_ object to use as starting point for _require_ calls | ||
* _msgmap_ : (optional) Object; map codes to message templates | ||
* _inspect_ : (optional) Boolean; If true, _util.inspect_ is called on values; default: true. | ||
* `package` : (_optional_) String; package name to mark Error objects | ||
* `prefix` : (_optional_) Boolean/String; If false, then no prefix is used; If not defined, the package name is used | ||
* `module` : (_optional_) Object; `module` object to use as starting point for `require` calls | ||
* `msgmap` : (_optional_) Object; map codes to message templates | ||
* `inspect` : (_optional_) Boolean; If true, `util.inspect` is called on values; default: true. | ||
@@ -196,2 +184,9 @@ | ||
[npm-badge]: https://badge.fury.io/js/eraro.svg | ||
[npm-url]: https://badge.fury.io/js/eraro | ||
[travis-badge]: https://api.travis-ci.org/rjrodger/eraro.svg | ||
[travis-url]: https://travis-ci.org/rjrodger/eraro | ||
[coveralls-badge]:https://coveralls.io/repos/rjrodger/eraro/badge.svg?branch=master&service=github | ||
[coveralls-url]: https://coveralls.io/github/rjrodger/eraro?branch=master | ||
[david-badge]: https://david-dm.org/rjrodger/eraro.svg | ||
[david-url]: https://david-dm.org/rjrodger/eraro |
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
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15396
4
232
0
4
181
2
+ Addedlodash@4.17.21(transitive)
- Removedlodash@2.4.2(transitive)
Updatedlodash@4.17