1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:17:34 +00:00

LibJS: Handle non-Error this object in Error.prototype.stack getter

This is taken from the abandoned error stacks proposal, which
already serves as the source of truth for the setter. It only requires
the this value to be an object - if it's not an Error object, the getter
returns undefined.
I have not compared this behavior to the non-standard implementations of
the stack property in other engines, but presumably the spec authors
already did that work.

This change gets the Sentry browser SDK working to a point where it can
actually send uncaught exceptions via the API :^)
This commit is contained in:
Linus Groh 2022-03-15 14:19:20 +00:00 committed by Andreas Kling
parent 37e988675f
commit fb60ada6ce
2 changed files with 32 additions and 7 deletions

View file

@ -1,4 +1,5 @@
const stackDescriptor = Object.getOwnPropertyDescriptor(Error.prototype, "stack");
const stackGetter = stackDescriptor.get;
const stackSetter = stackDescriptor.set;
describe("getter - normal behavior", () => {
@ -7,9 +8,9 @@ describe("getter - normal behavior", () => {
/^ at .*Error \(.*\/Error\.prototype\.stack\.js:\d+:\d+\)$/,
/^ at .+\/Error\/Error\.prototype\.stack\.js:\d+:\d+$/,
/^ at test \(.+\/test-common.js:557:21\)$/,
/^ at .+\/Error\/Error\.prototype\.stack\.js:5:33$/,
/^ at .+\/Error\/Error\.prototype\.stack\.js:6:33$/,
/^ at describe \(.+\/test-common\.js:534:21\)$/,
/^ at .+\/Error\/Error\.prototype\.stack\.js:4:38$/,
/^ at .+\/Error\/Error\.prototype\.stack\.js:5:38$/,
];
const values = [
{
@ -41,6 +42,19 @@ describe("getter - normal behavior", () => {
}
}
});
test("this value must be an object", () => {
expect(() => {
stackGetter.call("foo");
}).toThrowWithMessage(TypeError, "foo is not an object");
expect(() => {
stackGetter.call(42);
}).toThrowWithMessage(TypeError, "42 is not an object");
});
test("returns undefined when called with non-Error object this value", () => {
expect(stackGetter.call({})).toBeUndefined();
});
});
describe("setter - normal behavior", () => {