|
1 |
| -# Error.captureStackTrace |
| 1 | +# Error.captureStackTrace |
2 | 2 |
|
3 | 3 | **Stage**: [Stage 1](https://tc39.es/process-document/)
|
4 | 4 |
|
5 |
| -**Champion**: Matthew Gaudet (Mozilla) |
| 5 | +**Champions**: Matthew Gaudet (Mozilla), Daniel Minor (Mozilla) |
6 | 6 |
|
7 | 7 |
|
8 |
| -V8 has has a non-standard [Stack Trace API](https://v8.dev/docs/stack-trace-api) for a while. |
| 8 | +V8 has has a non-standard [Stack Trace API](https://v8.dev/docs/stack-trace-api) for a while. |
9 | 9 |
|
10 |
| -In August 2023, [JSC also shipped this method](https://github.com/WebKit/WebKit/commit/997e074bb35ed07b69c9b821141c91dd548e0d02) |
| 10 | +In August 2023, [JSC also shipped this method](https://github.com/WebKit/WebKit/commit/997e074bb35ed07b69c9b821141c91dd548e0d02) |
11 | 11 |
|
12 |
| -This method [has now turned into a web-compatability problem](https://bugzilla.mozilla.org/show_bug.cgi?id=1886820), and as a result we should now standardize it. |
| 12 | +This method [has now turned into a web-compatability problem](https://bugzilla.mozilla.org/show_bug.cgi?id=1886820), and as a result we should now standardize it. |
13 | 13 |
|
14 | 14 | ## `Error.captureStackTrace`
|
15 | 15 |
|
16 | 16 | To quote the [V8 documentation](https://v8.dev/docs/stack-trace-api):
|
17 | 17 |
|
18 | 18 | > Stack trace collection for custom exceptions
|
19 |
| -> |
| 19 | +> |
20 | 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 |
| -> |
| 21 | +> |
22 | 22 | > Error.captureStackTrace(error, constructorOpt)
|
23 |
| -> |
| 23 | +> |
24 | 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 |
| -> |
| 25 | +> |
26 | 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 |
| -> |
| 27 | +> |
28 | 28 | > function MyError() {
|
29 | 29 | > Error.captureStackTrace(this, MyError);
|
30 | 30 | > // Any other initialization goes here.
|
31 | 31 | > }
|
32 |
| -> |
| 32 | +> |
33 | 33 | > Passing in `MyError` as a second argument means that the constructor call to `MyError` won’t show up in the stack trace.
|
34 | 34 |
|
35 |
| -## Implementation Divergence |
| 35 | +## Implementation Divergence |
36 | 36 |
|
37 |
| -Unfortunately, the JSC implementation diverges from the V8 one: |
| 37 | +Unfortunately, the JSC implementation diverges from the V8 one: |
38 | 38 |
|
39 | 39 | - JSC attaches a string valued prop to the object provided, where V8 instead installs their stack-getter function.
|
40 | 40 | - It uses the JSC stack string format.
|
41 | 41 |
|
42 |
| -I would actually like to specify this like the JSC one. It's simpler. Similarly, the text for the contents of the stack string should probably be something along the lines of |
| 42 | +I would actually like to specify this like the JSC one. It's simpler. Similarly, the text for the contents of the stack string should probably be something along the lines of |
43 | 43 |
|
44 | 44 | > The contents of the stack string is a textual representation of the [execution context stack](https://tc39.es/ecma262/#execution-context-stack), however the actual format and contents are implemetation defined and should not be relied upon to be identical across implementations.
|
45 | 45 |
|
46 | 46 |
|
47 | 47 | ## Related Work
|
48 | 48 |
|
49 | 49 | - The [Error Stacks](https://github.com/tc39/proposal-error-stacks) proposal is largely an orthogonal one to this, but it would provide framework and text to talk about stack strings, as mostly the current spec doesn't really talk about stacks. However, for this proposal I'd argue we don't need to specify the contents of stack strings.
|
50 |
| -- The [Error Stack Accessor](https://github.com/tc39/proposal-error-stack-accessor) proposal could be the other route by which the spec starts to talk about stacks. |
| 50 | +- The [Error Stack Accessor](https://github.com/tc39/proposal-error-stack-accessor) proposal could be the other route by which the spec starts to talk about stacks. |
51 | 51 |
|
52 | 52 | ## History
|
53 |
| -- Presented February 2025, and achieved Stage 1 [(slides)](https://docs.google.com/presentation/d/1SFdS9n5JR7Jqz29s7ApvkqDOqOfPW-IaBR2orK828As/mobilepresent?slide=id.p) (notes not yet posted) |
| 53 | +- Presented February 2025, and achieved Stage 1 [notes](https://github.com/tc39/notes/blob/main/meetings/2025-02/february-19.md#errorcapturestacktrace-for-stage-1) |
0 commit comments