better-try-catch
Advanced tools
Comparing version 0.6.1 to 0.6.2
61
index.js
@@ -0,1 +1,3 @@ | ||
/* eslint-disable no-unused-vars, no-eval */ | ||
'use strict' | ||
@@ -6,38 +8,45 @@ | ||
function onFulfilled(result) { | ||
return [null, result] | ||
return [ null, result ] | ||
} | ||
function onRejected(err) { | ||
return [err] | ||
return [ err ] | ||
} | ||
function getFnName(fn) { | ||
return (fn.name || '').replace(/\s|bound(?!$)/g, '') | ||
} | ||
function betterTryCatch(fn) { | ||
return function () { | ||
try { | ||
var result = fn.apply(this, arguments) | ||
return isPromise(result) | ||
? result.then(onFulfilled, onRejected) | ||
: [null, result] | ||
} catch (err) { | ||
return [err] | ||
} | ||
} | ||
var name = getFnName(fn) | ||
return eval('(function ' + name + '() {\n' + | ||
' try {\n' + | ||
' var result = fn.apply(this, arguments)\n' + | ||
' return isPromise(result)\n' + | ||
' ? result.then(onFulfilled, onRejected)\n' + | ||
' : [ null, result ]\n' + | ||
' } catch (err) {\n' + | ||
' return [ err ]\n' + | ||
' }\n' + | ||
'})') | ||
} | ||
function promisify(fn) { | ||
return function () { | ||
var ctx = this | ||
var len = arguments.length | ||
var i | ||
var args = new Array(len) | ||
for (i = 0; i < len; i++) | ||
args[i] = arguments[i] | ||
var name = getFnName(fn) | ||
return new Promise(function (resolve) { | ||
args[i] = function (err, result) { | ||
resolve(err ? [err] : [null, result]) | ||
} | ||
fn.apply(ctx, args) | ||
}) | ||
} | ||
return eval('(function ' + name + '() {\n' + | ||
' var self = this\n' + | ||
' var len = arguments.length\n' + | ||
' var i\n' + | ||
' var args = new Array(len)\n' + | ||
' for (i = 0; i < len; i++)\n' + | ||
' args[i] = arguments[i]\n' + | ||
' return new Promise(function (resolve) {\n' + | ||
' args[i] = function callback(err, result) {\n' + | ||
' resolve(err ? [ err ] : [ null, result ])\n' + | ||
' }\n' + | ||
' fn.apply(self, args)\n' + | ||
' })\n' + | ||
'})') | ||
} | ||
@@ -44,0 +53,0 @@ |
{ | ||
"name": "better-try-catch", | ||
"version": "0.6.1", | ||
"version": "0.6.2", | ||
"description": "Try-catch wrapper for easy error handling.", | ||
@@ -8,3 +8,5 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "tap test.js --coverage", | ||
"lint": "eslint .", | ||
"tests-only": "tap test/index.js --coverage", | ||
"test": "npm run lint && npm run tests-only", | ||
"codecov": "tap --coverage-report=lcov && codecov" | ||
@@ -33,4 +35,7 @@ }, | ||
"codecov": "^2.1.0", | ||
"eslint": "^4.0.0", | ||
"eslint-config-riophae": "0.0.1", | ||
"eslint-plugin-import": "^2.3.0", | ||
"tap": "^10.3.2" | ||
} | ||
} |
@@ -37,3 +37,3 @@ # better-try-catch | ||
## I didn't see it's "better"? | ||
## I didn't see it's "better?" | ||
@@ -63,3 +63,3 @@ Consider a situation like this: | ||
} catch (ex) { | ||
err = ex // Notice here | ||
err = ex // Note here | ||
// Also, we can not simply place the `if` statement inside the catch block | ||
@@ -66,0 +66,0 @@ // Since if no errors occured then the `if` statement will not execute at all |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
6144
44
5
2