This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from sumeetkakkar/fix/wrapped-error
fix: handle case where Error class is wrapped
- Loading branch information
Showing
5 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.idea | ||
node_modules | ||
*.log | ||
package-lock.json | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Unreleased | ||
|
||
- fix: handle case where Error class is wrapped #10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
function copyConstructorProperties(target, source) { | ||
const keys = Object.getOwnPropertyNames(source); | ||
for (let i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (!target.hasOwnProperty(key)) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
} | ||
} | ||
} | ||
// inspired from https://github.com/zloirock/core-js/blob/master/packages/core-js/internals/wrap-error-constructor-with-cause.js | ||
function wrapErrorConstructor(ERROR_NAME, wrapper) { | ||
const clearErrorStack = (function () { | ||
const TEST = (function (arg) { return String(Error(arg).stack); })('zxcasd'); | ||
const V8_OR_CHAKRA_STACK_ENTRY = /\n\s*at [^:]*:[^\n]*/; | ||
const IS_V8_OR_CHAKRA_STACK = V8_OR_CHAKRA_STACK_ENTRY.test(TEST); | ||
return function clearErrorStackInner(stack, dropEntries) { | ||
if (IS_V8_OR_CHAKRA_STACK && typeof stack == 'string') { | ||
while (dropEntries--) stack = stack.replace(V8_OR_CHAKRA_STACK_ENTRY, ''); | ||
} return stack; | ||
}; | ||
})(); | ||
|
||
const OriginalError = globalThis[ERROR_NAME]; | ||
|
||
const OriginalErrorPrototype = OriginalError.prototype; | ||
|
||
const WrappedError = wrapper(function (a) { | ||
const message = String(a); | ||
const result = new OriginalError(); | ||
if (message !== undefined) Object.defineProperty(result, 'message', { value: message, enumerable: false, configurable: true, writable: true }); | ||
Object.defineProperty(result, 'stack', { value: clearErrorStack(result.stack, 2), enumerable: false, configurable: true, writable: true }); | ||
// if (this && OriginalErrorPrototype.isPrototypeOf(this)) { | ||
// // inheritIfRequired(result, this, WrappedError); | ||
// Object.setPrototypeOf(result, this.constructor); //?? | ||
// } | ||
return result; | ||
}); | ||
|
||
WrappedError.prototype = OriginalErrorPrototype; | ||
|
||
// Copy ownKeys from OriginalError to WrappedError | ||
copyConstructorProperties(WrappedError, OriginalError); | ||
|
||
globalThis[ERROR_NAME] = WrappedError; | ||
}; | ||
|
||
module.exports = function wrapError() { | ||
const ErrorClassName = 'Error'; | ||
const OriginalError = globalThis[ErrorClassName]; | ||
wrapErrorConstructor(ErrorClassName, function (init) { | ||
return function Error(message) { return init.apply(this, arguments); }; | ||
}); | ||
|
||
return function restore() { | ||
globalThis[ErrorClassName] = OriginalError; | ||
}; | ||
}; |