88
99Properly update an error's stack.
1010
11- Work in progress!
11+ In V8 (Chrome, Node.js, Deno, etc.),
12+ [ ` error.stack ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack )
13+ includes
14+ [ ` error.message ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message ) .
15+ However, if ` error.stack ` is modified, ` error.message ` is not updated
16+ accordingly. This library fixes it.
1217
13- # Features
18+ On other JavaScript engines, this library just sets ` error.stack ` .
1419
1520# Example
1621
22+ Without ` set-error-stack ` :
23+
24+ ``` js
25+ const error = new Error (' one' )
26+ console .log (error .stack ) // 'Error: one ...'
27+ console .log (error .message ) // 'one'
28+
29+ error .stack = error .stack .replace (' one' , ' two' )
30+ console .log (error .stack ) // 'Error: two ...'
31+ console .log (error .message ) // 'one'
32+ ```
33+
34+ With ` set-error-stack ` :
35+
1736``` js
1837import setErrorStack from ' set-error-stack'
38+
39+ const error = new Error (' one' )
40+ console .log (error .stack ) // 'Error: one ...'
41+ console .log (error .message ) // 'one'
42+
43+ setErrorStack (stack, error .stack .replace (' one' , ' two' ))
44+ console .log (error .stack ) // 'Error: two ...'
45+ console .log (error .message ) // 'two'
1946```
2047
2148# Install
@@ -30,31 +57,48 @@ It is an ES module and must be loaded using
3057[ an ` import ` or ` import() ` statement] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) ,
3158not ` require() ` .
3259
33- <!--
34- This package works in Node.js >=14.18.0. It is an ES module and must be loaded
35- using
36- [an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c),
37- not `require()`.
38- -->
39-
4060# API
4161
42- ## setErrorStack(value, options? )
62+ ## setErrorStack(error, stack )
4363
44- ` value ` ` any ` \
45- ` options ` [ ` Options? ` ] ( #options ) \
46- _ Return value_ : [ ` object ` ] ( #return-value )
64+ ` error ` ` Error | any` \
65+ ` stack ` ` string ` \
66+ _ Return value_ : ` Error `
4767
48- ### Options
68+ Sets ` error.stack = stack ` . If needed, also modifies ` error.message `
69+ accordingly.
4970
50- Object with the following properties.
51-
52- ### Return value
53-
54- Object with the following properties.
71+ Returns ` error ` . If ` error ` is not an ` Error ` instance, it is converted to one.
5572
5673# Related projects
5774
75+ - [ ` modern-errors ` ] ( https://github.com/ehmicky/modern-errors ) : Handle errors
76+ like it's 2022 🔮
77+ - [ ` error-custom-class ` ] ( https://github.com/ehmicky/error-custom-class ) : Create
78+ one error class
79+ - [ ` error-class-utils ` ] ( https://github.com/ehmicky/error-class-utils ) : Utilities
80+ to properly create error classes
81+ - [ ` error-serializer ` ] ( https://github.com/ehmicky/error-serializer ) : Convert
82+ errors to/from plain objects
83+ - [ ` normalize-exception ` ] ( https://github.com/ehmicky/normalize-exception ) :
84+ Normalize exceptions/errors
85+ - [ ` is-error-instance ` ] ( https://github.com/ehmicky/is-error-instance ) : Check if
86+ a value is an ` Error ` instance
87+ - [ ` merge-error-cause ` ] ( https://github.com/ehmicky/merge-error-cause ) : Merge an
88+ error with its ` cause `
89+ - [ ` set-error-class ` ] ( https://github.com/ehmicky/set-error-class ) : Properly
90+ update an error's class
91+ - [ ` set-error-message ` ] ( https://github.com/ehmicky/set-error-message ) : Properly
92+ update an error's message
93+ - [ ` set-error-props ` ] ( https://github.com/ehmicky/set-error-props ) : Properly
94+ update an error's properties
95+ - [ ` error-cause-polyfill ` ] ( https://github.com/ehmicky/error-cause-polyfill ) :
96+ Polyfill ` error.cause `
97+ - [ ` handle-cli-error ` ] ( https://github.com/ehmicky/handle-cli-error ) : 💣 Error
98+ handler for CLI applications 💥
99+ - [ ` log-process-errors ` ] ( https://github.com/ehmicky/log-process-errors ) : Show
100+ some ❤ to Node.js process errors
101+
58102# Support
59103
60104For any question, _ don't hesitate_ to [ submit an issue on GitHub] ( ../../issues ) .
0 commit comments