error-ninja
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -10,3 +10,3 @@ { | ||
"name": "error-ninja", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Allows us to pre-define error types and error messages in each module.", | ||
@@ -13,0 +13,0 @@ "keywords": [ |
@@ -17,3 +17,3 @@ 'use strict'; | ||
const errorConstructorOptions = { | ||
const setupOptions = { | ||
stackTraceLimit: 20, | ||
@@ -24,2 +24,6 @@ fullInsight: void (0), | ||
const errorConstructorOptions = { | ||
fullInsight: setupOptions.fullInsight, | ||
}; | ||
// Set the number of frames in stack traces from this point onwards. | ||
@@ -51,5 +55,3 @@ Error.stackTraceLimit = options.stackTraceLimit; | ||
super(errorName, id, msg, data, { | ||
fullInsight: errorConstructorOptions.fullInsight, | ||
}); | ||
super(errorName, id, msg, data, errorConstructorOptions); | ||
@@ -64,3 +66,3 @@ // Remove this constructor from the stack trace. | ||
// Attach a static "chain()" method bound to the error class and error name to allow "ErrorClass.chain()". | ||
ErrorClass.chain = ErrorClass.__chainNinja.bind(null, ErrorClass, errorName); | ||
ErrorClass.chain = ErrorClass.__chainNinja.bind(null, ErrorClass, errorName, errorConstructorOptions); | ||
@@ -67,0 +69,0 @@ // Return the constructor for the new error class. |
@@ -181,30 +181,22 @@ 'use strict'; | ||
/** | ||
* Wraps the given raw error in the given error class. | ||
* Wraps the given native error in the given error class. | ||
* | ||
* @param {ErrorClass} ErrorClass - The error class we should wrap the error in. | ||
* @param {Error} rawError - The original raw JS error. | ||
* @param {Error} nativeError - The original raw native JS error. | ||
* @param {dictionary} errorConstructorOptions - The original options passed to the ErrorClass constructor. | ||
* @returns {ErrorClass} - The raw error wrapped in the error class. | ||
*/ | ||
static __wrapRawError (ErrorClass, rawError) { | ||
static __wrapNativeError (nativeError, errorConstructorOptions) { | ||
// Create the new error nina with the details of the raw error. | ||
const wrappedErr = new ErrorClass(rawError.message); | ||
wrappedErr.name = rawError.constructor.name; | ||
wrappedErr.__isWrapped = true; | ||
// Modify the raw error with the details of the error class. | ||
nativeError.__options = { ...errorConstructorOptions }; | ||
nativeError.id = null; | ||
nativeError.__originalMsg = nativeError.message; | ||
nativeError.__chain = []; | ||
nativeError.__isErrorNinja = true; | ||
nativeError.__isWrapped = true; | ||
nativeError.__isLite = false; | ||
nativeError.data = {}; | ||
// Remove this constructor from stack trace. | ||
Error.captureStackTrace(wrappedErr, ErrorClass.__wrapRawError); | ||
return nativeError; | ||
// Port across the native Node.js error properties on SystemError()'s, if they exist. | ||
wrappedErr.address = rawError.address; | ||
wrappedErr.code = rawError.code; | ||
wrappedErr.dest = rawError.dest; | ||
wrappedErr.errno = rawError.errno; | ||
wrappedErr.info = rawError.info; | ||
wrappedErr.path = rawError.path; | ||
wrappedErr.port = rawError.port; | ||
wrappedErr.syscall = rawError.syscall; | ||
return wrappedErr; | ||
} | ||
@@ -229,2 +221,3 @@ | ||
* @param {String} errorName - The class name for the error we are calling this method for. | ||
* @param {dictionary} errorConstructorOptions - The original options passed to the ErrorClass constructor. | ||
* @param {ErrorClass} originalError - The error that we need to chain this new error to. | ||
@@ -237,13 +230,21 @@ * @param {String} id - A short ID to identify the error. | ||
*/ | ||
static __chainNinja (ErrorClass, errorName, originalError, id, msg, data) { | ||
static __chainNinja (ErrorClass, errorName, errorConstructorOptions, originalError, id, msg, data) { | ||
const originalIsNinja = originalError.__isErrorNinja; | ||
const originalNinja = (originalIsNinja ? originalError : ErrorClass.__wrapRawError(ErrorClass, originalError)); | ||
let originalNinja; | ||
// We have a ninja error! | ||
if (originalError.__isErrorNinja) { | ||
originalNinja = originalError; | ||
} | ||
// We have a native error that needs wrapping. | ||
else { | ||
originalNinja = ErrorClass.__wrapNativeError(originalError, errorConstructorOptions); | ||
} | ||
// Create the new error and remove this constructor from stack trace. | ||
const newError = new NinjaError(errorName, id, msg, data, originalNinja.__options); | ||
// Remove this constructor from stack trace. | ||
Error.captureStackTrace(originalNinja, ErrorClass.__chainNinja); | ||
Error.captureStackTrace(newError, ErrorClass.__chainNinja); | ||
// Extend the chain! | ||
// Append the new error to the error chain. | ||
const fullInsightOption = originalNinja.__options.fullInsight; | ||
@@ -250,0 +251,0 @@ const newInsight = ErrorClass.__getInsightfulError(ErrorClass, fullInsightOption, originalNinja); |
340
24457