Skip to content

Commit 7737508

Browse files
committed
readme
1 parent c6dc2e9 commit 7737508

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22

33
Coerce an unknown error into an instance of the Error class
44

5+
## Examples
6+
7+
```ts
8+
try {
9+
doSomeWork();
10+
} catch (_err) {
11+
const err = coerceError(_err);
12+
console.log('Error:', err);
13+
}
14+
```
15+
16+
## Notes
17+
18+
`coerceError` does its best to maintain the stack trace of the thrown error.
19+
20+
If a non `Error` instance is thrown then JavaScript cannot infer the call-stack for it. For example:
21+
22+
``` ts
23+
try { throw 'untraceable'; }
24+
catch(error) { assert(typeof error === 'string')
25+
```
26+
27+
in this case, since a `string` is thrown and not an `Error` instance, there is no way to obtain the stack trace from the thrown point. Instead, `coerceError` will start the stack trace in the `catch` block.
28+
529
## Releasing a new version
630
731
To a release a new version:

0 commit comments

Comments
 (0)