Deno: cli/rt and op_crate/web - console output for ErrorEvent should be more informative

Created on 22 Oct 2020  路  5Comments  路  Source: denoland/deno

Imagine someone is writing a WebSocket client. MDN has a couple examples for handling WebSocket errors, e.g. in https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onerror and https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/error_event. Both just call console.log or console.error with the argument.

Consider this example:

const socket = new WebSocket('ws://localhost:8080');
socket.onerror = function(event) {
  console.error("WebSocket error observed:", event);
};

Running it as deno run test.js produces the following output:

WebSocket error observed: ErrorEvent { isTrusted: false }

Not very informative. https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent does list the error property, but it's not easily discoverable (there are no links to it from any WebSocket documentation pages as far as I can tell) and its significance isn't obvious.

cli good first issue op_cratweb 馃摝 suggestion

All 5 comments

There are a couple issues here... first in some cases WebSocket isn't actually raising an EventError, but just a plain Event:

https://github.com/denoland/deno/blob/c5c48f845a4d25f064c4388fcdd4295317edf155/cli/rt/27_websocket.js#L64

and

https://github.com/denoland/deno/blob/c5c48f845a4d25f064c4388fcdd4295317edf155/cli/rt/27_websocket.js#L87

and

https://github.com/denoland/deno/blob/c5c48f845a4d25f064c4388fcdd4295317edf155/cli/rt/27_websocket.js#L292

In addition, while our Event implementation is spec compliant, most browsers implement some sort of custom inspect to output non-owned properties on the event. For you get the following in Chrome:

> new ErrorEvent("error", { error: new TypeError("hello"), message: "hello" })
ErrorEvent聽{isTrusted: false, message: "hello", filename: "", lineno: 0, colno: 0,聽鈥

Hi. Could I please have a go at this one?

@ross-weir Sure! All help is appreciated :-)

This appears to be affecting all events:

Deno:

> new Event("testing")
Event { isTrusted: false }

Chrome:

> new Event("testing)
Event聽{isTrusted: false, type: "testing", target: null, currentTarget: null, eventPhase: 0,聽鈥

I'm not knowledgeable enough to come up with a solution/pinpoint the problem but I've found that it might have something to do with the properties being defined on the prototype - isTrusted is the only property defined on the instance.

I would be happy to continue working on this with some guidance :-)

That is aligned to the spec, that they are not defined on the instance.

There is a symbol of "Deno.customInspect" that allows you to write a method that would output something more informative. You can see a few examples here and here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xueqingxiao picture xueqingxiao  路  3Comments

kitsonk picture kitsonk  路  3Comments

ry picture ry  路  3Comments

doutchnugget picture doutchnugget  路  3Comments

zugende picture zugende  路  3Comments