@bemoje/stacktrace 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -0
- package/index.mjs +4 -4
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# @bemoje/stacktrace
|
|
2
|
+
|
|
3
|
+
Beautiful, color-formatted stack trace output for Node.js errors with syntax-highlighted file paths and structured
|
|
4
|
+
error properties.
|
|
5
|
+
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](https://nodejs.org/api/esm.html)
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @bemoje/stacktrace
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Pretty Print an Error
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { prettyStackTrace } from '@bemoje/stacktrace'
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
throw new Error('Something went wrong')
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error(prettyStackTrace(error as Error))
|
|
26
|
+
}
|
|
27
|
+
// Error: Something went wrong (red)
|
|
28
|
+
// stack:
|
|
29
|
+
// functionName src/index.ts:12:5 (source files highlighted in red)
|
|
30
|
+
// otherMethod node_modules/... (dependencies in yellow)
|
|
31
|
+
// internal node:internal/... (node internals in gray)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Enable Globally for Uncaught Exceptions
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { enablePrettyStackTrace } from '@bemoje/stacktrace'
|
|
38
|
+
|
|
39
|
+
enablePrettyStackTrace()
|
|
40
|
+
// All uncaught exceptions will now be printed with color-formatted stack traces
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Options
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { prettyStackTrace } from '@bemoje/stacktrace'
|
|
47
|
+
|
|
48
|
+
const error = new Error('fail')
|
|
49
|
+
|
|
50
|
+
// Omit stack trace, show only message and properties
|
|
51
|
+
prettyStackTrace(error, { omitStack: true })
|
|
52
|
+
|
|
53
|
+
// Omit custom properties, show only message and stack
|
|
54
|
+
prettyStackTrace(error, { omitProps: true })
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Custom Error Properties
|
|
58
|
+
|
|
59
|
+
Extra properties on error objects are automatically displayed:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
const error = new Error('Validation failed')
|
|
63
|
+
;(error as any).field = 'email'
|
|
64
|
+
;(error as any).value = 'not-an-email'
|
|
65
|
+
|
|
66
|
+
console.error(prettyStackTrace(error))
|
|
67
|
+
// Error: Validation failed
|
|
68
|
+
// { field: 'email', value: 'not-an-email' }
|
|
69
|
+
// stack:
|
|
70
|
+
// ...
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## API Reference
|
|
74
|
+
|
|
75
|
+
| Export | Description |
|
|
76
|
+
| ------------------------ | ------------------------------------------------------------------- |
|
|
77
|
+
| `prettyStackTrace` | Format an Error into a colorized, readable string |
|
|
78
|
+
| `enablePrettyStackTrace` | Register a global uncaught exception handler with pretty formatting |
|
package/index.mjs
CHANGED
|
@@ -28,7 +28,7 @@ function renderStack(error, options) {
|
|
|
28
28
|
}, 0);
|
|
29
29
|
const stack = frames.map((frame) => {
|
|
30
30
|
if (frame.file) {
|
|
31
|
-
frame.file = upath.relative(process.cwd(), frame.file.replace(/^file:[
|
|
31
|
+
frame.file = upath.relative(process.cwd(), frame.file.replace(/^file:[/\\]+/, ""));
|
|
32
32
|
}
|
|
33
33
|
const { methodName, file, lineNumber, column } = frame;
|
|
34
34
|
let s = " ";
|
|
@@ -60,14 +60,14 @@ function renderProps(error, options) {
|
|
|
60
60
|
if (options.omitProps) {
|
|
61
61
|
return "";
|
|
62
62
|
}
|
|
63
|
-
const ignore = [
|
|
63
|
+
const ignore = /* @__PURE__ */ new Set([
|
|
64
64
|
"name",
|
|
65
65
|
"message",
|
|
66
66
|
"frames",
|
|
67
67
|
"stack"
|
|
68
|
-
];
|
|
68
|
+
]);
|
|
69
69
|
const keys = Object.getOwnPropertyNames(error).filter((key) => {
|
|
70
|
-
return !ignore.
|
|
70
|
+
return !ignore.has(key);
|
|
71
71
|
});
|
|
72
72
|
if (!keys.length) {
|
|
73
73
|
return "";
|