Jonas Sicking (2013-07-10T16:23:54.000Z)
Hi all,

What is the current expected way for an author to throw a custom
errors? You obviously could do something like:

throw { reason: "TimeoutError" };
or
throw "TimeoutError";

However that makes it very hard for anyone receiving an exception to
inspect what it is. I.e. you'd first have to check what type of value
it is, i.e. if it's a string, something that's instanceof Error, or
something with just custom properties.

Instead you can do

throw new Error;

However, as I understand it, that doesn't let you pass
machine-readable information in the error. You can only pass a message
which is intended for human consumption. I.e. something like:

throw new Error("operation took too long and timed out.");

Yet another alternative is to do

let e = new Error;
e.name = "TimeoutError";
throw e;

This is a little bit more code than expected though.

A shorter alternative that is somewhat compatible with Error is

throw { name: "TimeoutError" };

However this means that 'instanceof Error' returns false, which could
mean that code is forced to check which properties are set.

The reason I'm asking is that the DOM has invented a completely new
interface, DOMError. This seems pretty heavy-handed and yet another
instance of the DOM doing it in its own way rather than using existing
ES functionality.

I'd like for the DOM to mimic what we expect authors to do. It's just
not obvious to me what authors are expected to do if they want to
throw a machine readable error. I.e. one that allows code to catch the
error and handle it.

/ Jonas
github at esdiscuss.org (2013-07-12T02:27:45.712Z)
What is the current expected way for an author to throw a custom
errors? You obviously could do something like:

```js
throw { reason: "TimeoutError" };
```

or

```js
throw "TimeoutError";
```

However that makes it very hard for anyone receiving an exception to
inspect what it is. I.e. you'd first have to check what type of value
it is, i.e. if it's a string, something that's `instanceof Error`, or
something with just custom properties.

Instead you can do

```js
throw new Error;
```

However, as I understand it, that doesn't let you pass
machine-readable information in the error. You can only pass a message
which is intended for human consumption. I.e. something like:

```js
throw new Error("operation took too long and timed out.");
```

Yet another alternative is to do

```js
let e = new Error;
e.name = "TimeoutError";
throw e;
```

This is a little bit more code than expected though.

A shorter alternative that is somewhat compatible with Error is

```js
throw { name: "TimeoutError" };
```

However this means that `instanceof Error` returns false, which could
mean that code is forced to check which properties are set.

The reason I'm asking is that the DOM has invented a completely new
interface, `DOMError`. This seems pretty heavy-handed and yet another
instance of the DOM doing it in its own way rather than using existing
ES functionality.

I'd like for the DOM to mimic what we expect authors to do. It's just
not obvious to me what authors are expected to do if they want to
throw a machine readable error. I.e. one that allows code to catch the
error and handle it.