core-error-predicates
Advanced tools
Comparing version 1.0.3 to 1.1.0
36
index.js
@@ -8,3 +8,3 @@ var errors = []; | ||
name: name, | ||
predicate: function(e) { | ||
class: createClass(name, code, function(e) { | ||
if (!e) return false; | ||
@@ -18,4 +18,30 @@ if ("cause" in e && typeof e.cause.code === "string") { | ||
} | ||
}) | ||
}); | ||
} | ||
function createClass(name, defaultCode, predicate) { | ||
var CustomError = (new Function('name', 'defaultCode', 'predicate', [ | ||
'"use strict";', | ||
'return function ' + name + '(message, code) {', | ||
' if (this === undefined) return message instanceof ' + name + ' ? true : predicate(message);', | ||
' Error.captureStackTrace(this, "' + name + '");', | ||
' Error.call(this);', | ||
' this.name = name;', | ||
' this.message = message;', | ||
' this.code = code === undefined ? defaultCode : code;', | ||
'}' | ||
].join('\n')))(name, defaultCode, predicate); | ||
CustomError.prototype = Object.create(Error.prototype, { | ||
constructor: { | ||
value: CustomError, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
Object.setPrototypeOf(CustomError, Error); | ||
return CustomError; | ||
} | ||
@@ -50,3 +76,3 @@ | ||
name: "SSLError", | ||
predicate: function(e) { | ||
class: createClass("SSLError", ["SSLERROR"], function(e) { | ||
var re = /^SSL Error:/; | ||
@@ -61,3 +87,3 @@ if (!e) return false; | ||
} | ||
} | ||
}) | ||
}); | ||
@@ -67,3 +93,3 @@ | ||
errors.forEach(function(error) { | ||
module.exports[error.name] = error.predicate; | ||
module.exports[error.name] = error.class; | ||
}); | ||
@@ -74,3 +100,3 @@ | ||
if (global[error.name] === undefined) { | ||
global[error.name] = error.predicate; | ||
global[error.name] = error.class; | ||
} | ||
@@ -77,0 +103,0 @@ }); |
{ | ||
"name": "core-error-predicates", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Error predicate functions for operational errors thrown by node core", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -32,2 +32,13 @@ #Installation | ||
Predicates can be turned into instances of their error types by using the `new` operator: | ||
```js | ||
try { | ||
throw new FileNotFoundError("A file was not found!"); | ||
} | ||
catch (e) { | ||
assert(FileNotFoundError(e) === true); | ||
} | ||
``` | ||
##Error predicates | ||
@@ -34,0 +45,0 @@ |
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
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
10250
90
210