├── .gitattributes ├── .gitignore ├── .npmrc ├── LICENSE ├── README.md ├── index.html ├── package.json └── spec.emu /.gitattributes: -------------------------------------------------------------------------------- 1 | index.html -diff merge=ours 2 | 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | 29 | # Only apps should have lockfiles 30 | npm-shrinkwrap.json 31 | package-lock.json 32 | yarn.lock 33 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jordan Harband 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Error.isError]() 2 | ECMAScript Proposal, specs, and reference implementation for `Error.isError` 3 | 4 | Spec drafted by [@ljharb](https://github.com/ljharb). 5 | 6 | This proposal is currently Stage 4 of the [process](https://tc39.github.io/process-document/). 7 | 8 | ## Rationale 9 | I brought up concerns to the committee about `Symbol.toStringTag`, and how previously reliable and unspoofable `Object#toString` calls would now no longer be reliable. The committee consensus was that as long as there were prototype methods for all builtins that, at the least, threw an error when an internal slot was not present, that would be sufficient to serve as a reliable branding test. 10 | 11 | However, the internal slot for `Error` instances (and its subclasses) is only checked in `Object#toString` itself - leaving no possible reliable test in a world with `Symbol.toStringTag`. 12 | 13 | `instanceof Error`, of course, is unreliable because it will provide a false negative with a cross-realm (eg, from an iframe, or node's `vm` modules) `Error` instance. 14 | 15 | ## Use cases 16 | 17 | This list is not exhaustive. 18 | 19 | - debugging: it is very helpful to humans, even if not always to programs, to know what kind of thing a value is. Knowing if something is a "real" native error is thus valuable information to make available, including to error-reporting libraries. 20 | - serialization: platforms such as [RunKit](https://runkit.com/) need to serialize values safely and reconstruct them or describe them in the user’s browser. brand checking is critical for this. 21 | - structuredClone: this HTML method, which is also in node, brand-checks, and has special behavior for native Errors. JS programs need a way to know in advance if this behavior will be applied 22 | - centralized error collection: reporting errors from an iframe (w/ same origin) to a top frame where all errors are collected, evaluated, and reported to a personalized server to be investigated at a later time. However, a `throw` statement can throw any exception with an unknown type of data, and a verification is required for the data to be collected. The `thrownData instanceof Error` doesn't satisfy `Error` because the thrown error will inherit the `Error` instance of another realm, meaning `contentWindow.Error` is the abstract and `thrownData instanceof document.getElementsByTagName('iframe')[0].contentWindow.Error` is required to satisfy the validation. 23 | 24 | ## Spec 25 | You can view the spec rendered as [HTML](https://tc39.es/proposal-is-error/). 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ecma-proposal-is-error", 3 | "version": "0.0.0", 4 | "description": "ECMAScript spec proposal for Error.isError", 5 | "scripts": { 6 | "prepublish": "npm run build && echo >&2 'no publishing' && exit 255", 7 | "start": "npm run build-loose -- --watch", 8 | "build": "npm run build-loose -- --strict", 9 | "build-loose": "ecmarkup --load-biblio @tc39/ecma262-biblio --verbose spec.emu index.html --lint-spec" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/tc39/proposal-is-error.git" 14 | }, 15 | "keywords": [ 16 | "Error.isError", 17 | "Error", 18 | "TypeError", 19 | "ES7", 20 | "ECMAScript", 21 | "ESNext", 22 | "spec" 23 | ], 24 | "author": "Jordan Harband ", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/tc39/proposal-is-error/issues" 28 | }, 29 | "homepage": "https://github.com/tc39/proposal-is-error#readme", 30 | "devDependencies": { 31 | "@tc39/ecma262-biblio": "^2.1.2806", 32 | "ecmarkup": "^20.0.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /spec.emu: -------------------------------------------------------------------------------- 1 | 2 | 3 |
 4 | title: Error.isError
 5 | stage: 4
 6 | contributors: Jordan Harband
 7 | 
8 | 9 | 10 |

Fundamental Objects

11 | 12 | 13 |

Error Objects

14 | 15 | 16 |

Properties of the Error Constructor

17 | 18 | 19 |

Error.isError ( _arg_ )

20 | 21 | 22 | 1. Return IsError(_arg_). 23 | 24 |
25 |
26 | 27 | 28 |

Properties of Error Instances

29 | 30 |

Error instances are ordinary objects that inherit properties from the Error prototype object and have an [[ErrorData]] internal slot whose value is *undefined*. The only specified uses of [[ErrorData]] is to identify Error, AggregateError, and _NativeError_ instances as Error objects within `Object.prototype.toString` and `Error.isError`.

31 |
32 | 33 | 34 |

Properties of _NativeError_ Instances

35 |

_NativeError_ instances are ordinary objects that inherit properties from their _NativeError_ prototype object and have an [[ErrorData]] internal slot whose value is *undefined*. The only specified use of [[ErrorData]] is by `Object.prototype.toString` () and `Error.isError` () to identify Error, AggregateError, or _NativeError_ instances.

36 |
37 | 38 | 39 |

AggregateError Objects

40 | 41 | 42 |

Properties of AggregateError Instances

43 |

AggregateError instances are ordinary objects that inherit properties from their AggregateError prototype object and have an [[ErrorData]] internal slot whose value is *undefined*. The only specified use of [[ErrorData]] is by `Object.prototype.toString` () and `Error.isError` () to identify Error, AggregateError, or _NativeError_ instances.

44 |
45 |
46 | 47 | 48 |

Abstract Operations for Error Objects

49 | 50 | 51 |

52 | IsError( 53 | _argument_: an Ecmascript language value, 54 | ): a Boolean 55 |

56 |
57 |
description
58 |
It returns a boolean indicating whether the argument is a built-in Error instance or not.
59 |
60 | 61 | 62 | 1. If _argument_ is not an Object, return *false*. 63 | 1. If _argument_ has an [[ErrorData]] internal slot, return *true*. 64 | 1. Return *false*. 65 | 66 |
67 |
68 |
69 |
70 | --------------------------------------------------------------------------------