Skip to content

Commit 201085b

Browse files
committed
fix error events not working
1 parent e16a89c commit 201085b

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

.changeset/nasty-spoons-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@remote-dom/polyfill': patch
3+
---
4+
5+
fix error events not working
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Event} from './Event.ts';
2+
3+
// https://html.spec.whatwg.org/multipage/webappapis.html#errorevent
4+
export class ErrorEvent extends Event {
5+
readonly message: ErrorEventInit['message'];
6+
readonly filename: ErrorEventInit['filename'];
7+
readonly lineno: ErrorEventInit['lineno'];
8+
readonly colno: ErrorEventInit['colno'];
9+
readonly error: ErrorEventInit['error'];
10+
11+
constructor(type: string, eventInitDict: ErrorEventInit) {
12+
super(type, eventInitDict);
13+
14+
this.message = eventInitDict.message;
15+
this.filename = eventInitDict.filename;
16+
this.lineno = eventInitDict.lineno;
17+
this.colno = eventInitDict.colno;
18+
this.error = eventInitDict.error;
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Event} from './Event.ts';
2+
3+
//https://html.spec.whatwg.org/multipage/webappapis.html#promiserejectionevent
4+
export class PromiseRejectionEvent extends Event {
5+
readonly promise: PromiseRejectionEventInit['promise'];
6+
readonly reason: PromiseRejectionEventInit['reason'];
7+
8+
constructor(type: string, eventInitDict: PromiseRejectionEventInit) {
9+
super(type, eventInitDict);
10+
11+
this.promise = eventInitDict.promise;
12+
this.reason = eventInitDict.reason;
13+
}
14+
}

packages/polyfill/source/Window.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {Document} from './Document.ts';
22
import {Event} from './Event.ts';
33
import {EventTarget} from './EventTarget.ts';
44
import {CustomEvent} from './CustomEvent.ts';
5+
import {ErrorEvent} from './ErrorEvent.ts';
6+
import {PromiseRejectionEvent} from './PromiseRejectionEvent.ts';
57
import {Node} from './Node.ts';
68
import {ParentNode} from './ParentNode.ts';
79
import {ChildNode} from './ChildNode.ts';
@@ -30,6 +32,8 @@ export class Window extends EventTarget {
3032
location = globalThis.location;
3133
navigator = globalThis.navigator;
3234
Event = Event;
35+
ErrorEvent = ErrorEvent;
36+
PromiseRejectionEvent = PromiseRejectionEvent;
3337
EventTarget = EventTarget;
3438
CustomEvent = CustomEvent;
3539
Node = Node;
@@ -46,6 +50,20 @@ export class Window extends EventTarget {
4650
HTMLTemplateElement = HTMLTemplateElement;
4751
MutationObserver = MutationObserver;
4852

53+
get onerror() {
54+
return globalThis.onerror;
55+
}
56+
set onerror(handler: any) {
57+
globalThis.onerror = handler;
58+
}
59+
60+
get onunhandledrejection() {
61+
return globalThis.onunhandledrejection;
62+
}
63+
set onunhandledrejection(handler: any) {
64+
globalThis.onunhandledrejection = handler;
65+
}
66+
4967
static setGlobal(window: Window) {
5068
const properties = Object.getOwnPropertyDescriptors(window);
5169

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import {Window} from '../Window.ts';
2+
import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
3+
4+
describe('global errors', () => {
5+
describe('onerror', () => {
6+
let originalOnerror: any;
7+
8+
beforeEach(() => {
9+
originalOnerror = globalThis.onerror;
10+
});
11+
12+
afterEach(() => {
13+
globalThis.onerror = originalOnerror;
14+
});
15+
16+
it('sets onerror handler on window instance', () => {
17+
const handler = vi.fn();
18+
const window = new Window();
19+
20+
window.onerror = handler;
21+
expect(window.onerror).toBe(handler);
22+
});
23+
24+
it('sets onerror handler on globalThis when assigned to window', () => {
25+
const handler = vi.fn();
26+
const window = new Window();
27+
28+
window.onerror = handler;
29+
expect(globalThis.onerror).toBe(handler);
30+
});
31+
32+
it('allows null assignment', () => {
33+
const handler = vi.fn();
34+
const window = new Window();
35+
window.onerror = handler;
36+
expect(window.onerror).toBe(handler);
37+
expect(globalThis.onerror).toBe(handler);
38+
39+
window.onerror = null;
40+
expect(window.onerror).toBe(null);
41+
expect(globalThis.onerror).toBe(null);
42+
});
43+
44+
it('works when windowis is set as global', () => {
45+
const window = new Window();
46+
Window.setGlobalThis(window);
47+
const handler = vi.fn();
48+
window.onerror = handler;
49+
50+
expect(window.onerror).toBe(handler);
51+
expect(globalThis.onerror).toBe(handler);
52+
});
53+
});
54+
55+
describe('onunhandledrejection', () => {
56+
let originalOnunhandledrejection: any;
57+
58+
beforeEach(() => {
59+
originalOnunhandledrejection = globalThis.onunhandledrejection;
60+
});
61+
62+
afterEach(() => {
63+
globalThis.onunhandledrejection = originalOnunhandledrejection;
64+
});
65+
66+
it('sets onunhandledrejection handler on window instance', () => {
67+
const handler = vi.fn();
68+
const window = new Window();
69+
70+
window.onunhandledrejection = handler;
71+
expect(window.onunhandledrejection).toBe(handler);
72+
});
73+
74+
it('sets onunhandledrejection handler on globalThis when assigned to window', () => {
75+
const handler = vi.fn();
76+
const window = new Window();
77+
78+
window.onunhandledrejection = handler;
79+
expect(globalThis.onunhandledrejection).toBe(handler);
80+
});
81+
82+
it('allows null assignment', () => {
83+
const handler = vi.fn();
84+
const window = new Window();
85+
window.onunhandledrejection = handler;
86+
expect(window.onunhandledrejection).toBe(handler);
87+
expect(globalThis.onunhandledrejection).toBe(handler);
88+
89+
window.onunhandledrejection = null;
90+
expect(window.onunhandledrejection).toBe(null);
91+
expect(globalThis.onunhandledrejection).toBe(null);
92+
});
93+
94+
it('works when windowis is set as global', () => {
95+
const window = new Window();
96+
Window.setGlobalThis(window);
97+
const handler = vi.fn();
98+
window.onunhandledrejection = handler;
99+
100+
expect(window.onunhandledrejection).toBe(handler);
101+
expect(globalThis.onunhandledrejection).toBe(handler);
102+
});
103+
});
104+
});

0 commit comments

Comments
 (0)