|
1 |
| -# template-for-proposals |
| 1 | +# Error.captureStackTrace |
2 | 2 |
|
3 |
| -A repository template for ECMAScript proposals. |
| 3 | +**Stage**: [Stage 0](https://tc39.es/process-document/) |
| 4 | +**Champion**: Matthew Gaudet (Mozilla) |
4 | 5 |
|
| 6 | + |
| 7 | +V8 has has a non-standard [Stack Trace API](https://v8.dev/docs/stack-trace-api) for a while. |
| 8 | + |
| 9 | +In August 2023, [JSC also shipped this method](https://github.com/WebKit/WebKit/commit/997e074bb35ed07b69c9b821141c91dd548e0d02) |
| 10 | + |
| 11 | +Has now turned into a web-compatability problem, and as a result it is time it |
| 12 | +is standardized. |
| 13 | + |
| 14 | +## `Error.captureStackTrace` |
| 15 | + |
| 16 | +To quote the [V8 documentation](https://v8.dev/docs/stack-trace-api): |
| 17 | + |
| 18 | +> Stack trace collection for custom exceptions |
| 19 | +> |
| 20 | +> The stack trace mechanism used for built-in errors is implemented using a general stack trace collection API that is also available to user scripts. The function |
| 21 | +> |
| 22 | +> Error.captureStackTrace(error, constructorOpt) |
| 23 | +> |
| 24 | +> adds a stack property to the given error object that yields the stack trace at the time captureStackTrace was called. Stack traces collected through `Error.captureStackTrace` are immediately collected, formatted, and attached to the given error object. |
| 25 | +> |
| 26 | +> The optional `constructorOpt` parameter allows you to pass in a function value. When collecting the stack trace all frames above the topmost call to this function, including that call, are left out of the stack trace. This can be useful to hide implementation details that won’t be useful to the user. The usual way of defining a custom error that captures a stack trace would be: |
| 27 | +> |
| 28 | +> function MyError() { |
| 29 | +> Error.captureStackTrace(this, MyError); |
| 30 | +> // Any other initialization goes here. |
| 31 | +> } |
| 32 | +> |
| 33 | +> Passing in `MyError` as a second argument means that the constructor call to `MyError` won’t show up in the stack trace. |
| 34 | +
|
| 35 | +## Implementation Divergence |
| 36 | + |
| 37 | +Unfortunately, the JSC implementation diverges from the V8 one: |
| 38 | + |
| 39 | +- JSC attaches a string valued prop to the object provided, where V8 instead installs their stack-getter function. |
| 40 | + |
| 41 | +I would actually like to specify this like the JSC one. |
| 42 | + |
| 43 | +<!-- Comment out rest of this while I work on this |
5 | 44 | ## Before creating a proposal
|
6 | 45 |
|
7 | 46 | Please ensure the following:
|
|
0 commit comments