error-ninja
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -39,2 +39,4 @@ /* | ||
err.id = id; | ||
err.isError = true; | ||
err.isNinja = true; | ||
err.name = 'Error [' + id + ']'; | ||
@@ -53,1 +55,37 @@ err.message = message + dataStr; | ||
}; | ||
/* | ||
* Returns true if the given input is an error, or optionally an ErrorNinja. | ||
*/ | ||
ME.isError = function (err, ninjaOnly) { | ||
if (typeof ninjaOnly === 'undefined') { ninjaOnly = false; } | ||
// Must be an object first, there is no typeof 'error'. | ||
if (typeof err !== 'object') { return false; } | ||
// Must have a valid constructor name. | ||
var constructorName = err.constructor.name.tolowerCase(); | ||
if ( | ||
constructorName !== 'error' && | ||
constructorName !== 'RangeError' && | ||
constructorName !== 'ReferenceError' && | ||
constructorName !== 'TypeError' && | ||
constructorName !== 'SyntaxError' | ||
) { | ||
return false; | ||
} | ||
// Do we also need to be an ErrorNinja? | ||
if (ninjaOnly && (!err.isError || !err.isNinja)) { return false; } | ||
// Success! We have an error. | ||
return true; | ||
}; | ||
/* | ||
* Returns true if the given input is specifically an ErrorNinja. | ||
*/ | ||
ME.isNinja = function (err) { | ||
return ME.isError(err, true); | ||
}; |
@@ -10,3 +10,3 @@ { | ||
"name": "error-ninja", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Allows us to pre-define error types and error messages in each module.", | ||
@@ -13,0 +13,0 @@ "keywords": [ |
@@ -10,2 +10,3 @@ # Error-Ninja | ||
5. Throws just like a plain old JavaScript Error. | ||
6. Allows you to detect if a variable is a JavaScript error or an ErrorNinja error. | ||
@@ -85,6 +86,21 @@ ## Define Your Error Messages | ||
err.id; // "attachment-too-big" | ||
err.human; // "You cannot upload an attachment that big!" | ||
err.data; // (mixed) e.g. { fileSize: 17483, maxSize: 1024 } | ||
// data defaults to {} | ||
err.id; // "attachment-too-big" | ||
err.isError; // True | ||
err.isNinja; // True | ||
err.human; // "You cannot upload an attachment that big!" | ||
err.data; // (mixed) e.g. { fileSize: 17483, maxSize: 1024 } | ||
// data defaults to {} | ||
``` | ||
## How to Tell if a Variable is an Error? | ||
ErrorNinja provides two methods to detect if a variable is an error, or more specifically an ErrorNinja error. | ||
```javascript | ||
// Ordinary JavaScript errors. | ||
var err1 = new Error(); | ||
if (ErrorNinja.isError(err1)) { ... } | ||
// ErrorNinja errors. | ||
var err2 = new ErrorNinja('...'); | ||
if (ErrorNinja.isNinja(err2)) { ... } | ||
``` |
Sorry, the diff of this file is not supported yet
6899
70
104