Skip to content

Commit

Permalink
fix: Expose ErrorEvent globally (denoland#5222)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed May 11, 2020
1 parent 32aeec9 commit d16c739
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 31 deletions.
2 changes: 2 additions & 0 deletions cli/js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as promiseTypes from "./web/promise.ts";
import * as customEvent from "./web/custom_event.ts";
import * as domException from "./web/dom_exception.ts";
import * as domFile from "./web/dom_file.ts";
import * as errorEvent from "./web/error_event.ts";
import * as event from "./web/event.ts";
import * as eventTarget from "./web/event_target.ts";
import * as formData from "./web/form_data.ts";
Expand Down Expand Up @@ -227,6 +228,7 @@ export const windowOrWorkerGlobalScopeProperties = {
File: nonEnumerable(domFile.DomFileImpl),
CustomEvent: nonEnumerable(customEvent.CustomEventImpl),
DOMException: nonEnumerable(domException.DOMExceptionImpl),
ErrorEvent: nonEnumerable(errorEvent.ErrorEventImpl),
Event: nonEnumerable(event.EventImpl),
EventTarget: nonEnumerable(eventTarget.EventTargetImpl),
URL: nonEnumerable(url.URLImpl),
Expand Down
3 changes: 2 additions & 1 deletion cli/js/runtime_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import * as denoNs from "./deno.ts";
import * as denoUnstableNs from "./deno_unstable.ts";
import * as webWorkerOps from "./ops/web_worker.ts";
import { log, assert, immutableDefine } from "./util.ts";
import { MessageEvent, ErrorEvent } from "./web/workers.ts";
import { ErrorEventImpl as ErrorEvent } from "./web/error_event.ts";
import { MessageEvent } from "./web/workers.ts";
import { TextEncoder } from "./web/text_encoding.ts";
import * as runtime from "./runtime.ts";
import { internalObject, internalSymbol } from "./internals.ts";
Expand Down
68 changes: 68 additions & 0 deletions cli/js/web/error_event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import { EventImpl as Event } from "./event.ts";
import { defineEnumerableProps } from "./util.ts";

export class ErrorEventImpl extends Event implements ErrorEvent {
#message: string;
#filename: string;
#lineno: number;
#colno: number;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
#error: any;

get message(): string {
return this.#message;
}
get filename(): string {
return this.#filename;
}
get lineno(): number {
return this.#lineno;
}
get colno(): number {
return this.#colno;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get error(): any {
return this.#error;
}

constructor(
type: string,
{
bubbles,
cancelable,
composed,
message = "",
filename = "",
lineno = 0,
colno = 0,
error = null,
}: ErrorEventInit = {}
) {
super(type, {
bubbles: bubbles,
cancelable: cancelable,
composed: composed,
});

this.#message = message;
this.#filename = filename;
this.#lineno = lineno;
this.#colno = colno;
this.#error = error;
}

get [Symbol.toStringTag](): string {
return "ErrorEvent";
}
}

defineEnumerableProps(ErrorEventImpl, [
"message",
"filename",
"lineno",
"colno",
"error",
]);
31 changes: 1 addition & 30 deletions cli/js/web/workers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TextDecoder, TextEncoder } from "./text_encoding.ts";
/*
import { blobURLMap } from "./web/url.ts";
*/
import { ErrorEventImpl as ErrorEvent } from "./error_event.ts";
import { EventImpl as Event } from "./event.ts";
import { EventTargetImpl as EventTarget } from "./event_target.ts";

Expand Down Expand Up @@ -41,36 +42,6 @@ export class MessageEvent extends Event {
}
}

export interface ErrorEventInit extends EventInit {
message?: string;
filename?: string;
lineno?: number;
colno?: number;
error?: any;
}

export class ErrorEvent extends Event {
readonly message: string;
readonly filename: string;
readonly lineno: number;
readonly colno: number;
readonly error: any;

constructor(type: string, eventInitDict?: ErrorEventInit) {
super(type, {
bubbles: eventInitDict?.bubbles ?? false,
cancelable: eventInitDict?.cancelable ?? false,
composed: eventInitDict?.composed ?? false,
});

this.message = eventInitDict?.message ?? "";
this.filename = eventInitDict?.filename ?? "";
this.lineno = eventInitDict?.lineno ?? 0;
this.colno = eventInitDict?.colno ?? 0;
this.error = eventInitDict?.error ?? null;
}
}

function encodeMessage(data: any): Uint8Array {
const dataJson = JSON.stringify(data);
return encoder.encode(dataJson);
Expand Down

0 comments on commit d16c739

Please sign in to comment.