@bemoje/stacktrace 1.0.0 → 1.1.0
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/lib/enablePrettyStackTrace.cjs +14 -0
- package/lib/enablePrettyStackTrace.d.cts +6 -0
- package/lib/enablePrettyStackTrace.d.ts +6 -0
- package/lib/enablePrettyStackTrace.js +11 -0
- package/lib/index.cjs +19 -0
- package/lib/index.d.cts +2 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/prettyStackTrace.cjs +94 -0
- package/{index.d.ts → lib/prettyStackTrace.d.cts} +1 -6
- package/lib/prettyStackTrace.d.ts +9 -0
- package/{index.mjs → lib/prettyStackTrace.js} +6 -15
- package/package.json +70 -20
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 |
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var prettyStackTrace = require('./prettyStackTrace');
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
function enablePrettyStackTrace() {
|
|
8
|
+
process.prependOnceListener("uncaughtException", (e) => {
|
|
9
|
+
console.error(prettyStackTrace.prettyStackTrace(e));
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
__name(enablePrettyStackTrace, "enablePrettyStackTrace");
|
|
13
|
+
|
|
14
|
+
exports.enablePrettyStackTrace = enablePrettyStackTrace;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { __name } from './chunk-SHUYVCID.js';
|
|
2
|
+
import { prettyStackTrace } from './prettyStackTrace';
|
|
3
|
+
|
|
4
|
+
function enablePrettyStackTrace() {
|
|
5
|
+
process.prependOnceListener("uncaughtException", (e) => {
|
|
6
|
+
console.error(prettyStackTrace(e));
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
__name(enablePrettyStackTrace, "enablePrettyStackTrace");
|
|
10
|
+
|
|
11
|
+
export { enablePrettyStackTrace };
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var enablePrettyStackTrace = require('./enablePrettyStackTrace');
|
|
4
|
+
var prettyStackTrace = require('./prettyStackTrace');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Object.keys(enablePrettyStackTrace).forEach(function (k) {
|
|
9
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () { return enablePrettyStackTrace[k]; }
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
Object.keys(prettyStackTrace).forEach(function (k) {
|
|
15
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return prettyStackTrace[k]; }
|
|
18
|
+
});
|
|
19
|
+
});
|
package/lib/index.d.cts
ADDED
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var colors = require('ansi-colors');
|
|
4
|
+
var util = require('util');
|
|
5
|
+
var stacktraceParser = require('stacktrace-parser');
|
|
6
|
+
var upath = require('upath');
|
|
7
|
+
|
|
8
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
|
|
10
|
+
var colors__default = /*#__PURE__*/_interopDefault(colors);
|
|
11
|
+
var upath__default = /*#__PURE__*/_interopDefault(upath);
|
|
12
|
+
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
+
function prettyStackTrace(error, options = {}) {
|
|
16
|
+
return [
|
|
17
|
+
renderMessage(error),
|
|
18
|
+
renderProps(error, options),
|
|
19
|
+
renderStack(error, options),
|
|
20
|
+
""
|
|
21
|
+
].join("\n");
|
|
22
|
+
}
|
|
23
|
+
__name(prettyStackTrace, "prettyStackTrace");
|
|
24
|
+
function renderMessage(error) {
|
|
25
|
+
return colors__default.default.red(`${error.name}: ${error.message}`);
|
|
26
|
+
}
|
|
27
|
+
__name(renderMessage, "renderMessage");
|
|
28
|
+
function renderStack(error, options) {
|
|
29
|
+
if (!error.stack || options.omitStack) {
|
|
30
|
+
return "";
|
|
31
|
+
}
|
|
32
|
+
const frames = stacktraceParser.parse(error.stack);
|
|
33
|
+
const offset = frames.reduce((acc, frame) => {
|
|
34
|
+
return Math.max(acc, frame.methodName.length);
|
|
35
|
+
}, 0);
|
|
36
|
+
const stack = frames.map((frame) => {
|
|
37
|
+
if (frame.file) {
|
|
38
|
+
frame.file = upath__default.default.relative(process.cwd(), frame.file.replace(/^file:[/\\]+/, ""));
|
|
39
|
+
}
|
|
40
|
+
const { methodName, file, lineNumber, column } = frame;
|
|
41
|
+
let s = " ";
|
|
42
|
+
let fp = "";
|
|
43
|
+
if (file) {
|
|
44
|
+
const base = upath__default.default.basename(file);
|
|
45
|
+
if (file.startsWith("node:")) {
|
|
46
|
+
s += colors__default.default.gray(methodName);
|
|
47
|
+
fp = colors__default.default.gray(file);
|
|
48
|
+
} else if (file.includes("node_modules")) {
|
|
49
|
+
s += methodName;
|
|
50
|
+
fp = file.replace(base, colors__default.default.yellow(base));
|
|
51
|
+
} else {
|
|
52
|
+
s += methodName;
|
|
53
|
+
fp = file.replace(base, colors__default.default.red(base));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
s += " ".repeat(2 + offset - methodName.length);
|
|
57
|
+
s += `${fp}:${lineNumber}:${column}`;
|
|
58
|
+
return s;
|
|
59
|
+
});
|
|
60
|
+
return [
|
|
61
|
+
colors__default.default.magenta("stack:"),
|
|
62
|
+
...stack
|
|
63
|
+
].join("\n");
|
|
64
|
+
}
|
|
65
|
+
__name(renderStack, "renderStack");
|
|
66
|
+
function renderProps(error, options) {
|
|
67
|
+
if (options.omitProps) {
|
|
68
|
+
return "";
|
|
69
|
+
}
|
|
70
|
+
const ignore = /* @__PURE__ */ new Set([
|
|
71
|
+
"name",
|
|
72
|
+
"message",
|
|
73
|
+
"frames",
|
|
74
|
+
"stack"
|
|
75
|
+
]);
|
|
76
|
+
const keys = Object.getOwnPropertyNames(error).filter((key) => {
|
|
77
|
+
return !ignore.has(key);
|
|
78
|
+
});
|
|
79
|
+
if (!keys.length) {
|
|
80
|
+
return "";
|
|
81
|
+
}
|
|
82
|
+
const data = {};
|
|
83
|
+
for (const [k, v] of Object.entries(error)) {
|
|
84
|
+
if (keys.includes(k)) {
|
|
85
|
+
data[k] = v;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return util.inspect(data, {
|
|
89
|
+
colors: true
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
__name(renderProps, "renderProps");
|
|
93
|
+
|
|
94
|
+
exports.prettyStackTrace = prettyStackTrace;
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Enables pretty stack trace formatting for uncaught exceptions.
|
|
3
|
-
*/
|
|
4
|
-
declare function enablePrettyStackTrace(): void;
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* Formats stack traces with colors and improved readability for debugging.
|
|
8
3
|
*/
|
|
@@ -11,4 +6,4 @@ declare function prettyStackTrace(error: Error, options?: {
|
|
|
11
6
|
omitProps?: boolean;
|
|
12
7
|
}): string;
|
|
13
8
|
|
|
14
|
-
export {
|
|
9
|
+
export { prettyStackTrace };
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
+
import { __name } from './chunk-SHUYVCID.js';
|
|
1
2
|
import colors from 'ansi-colors';
|
|
2
3
|
import { inspect } from 'util';
|
|
3
4
|
import { parse } from 'stacktrace-parser';
|
|
4
5
|
import upath from 'upath';
|
|
5
6
|
|
|
6
|
-
var __defProp = Object.defineProperty;
|
|
7
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
7
|
function prettyStackTrace(error, options = {}) {
|
|
9
8
|
return [
|
|
10
9
|
renderMessage(error),
|
|
@@ -28,7 +27,7 @@ function renderStack(error, options) {
|
|
|
28
27
|
}, 0);
|
|
29
28
|
const stack = frames.map((frame) => {
|
|
30
29
|
if (frame.file) {
|
|
31
|
-
frame.file = upath.relative(process.cwd(), frame.file.replace(/^file:[
|
|
30
|
+
frame.file = upath.relative(process.cwd(), frame.file.replace(/^file:[/\\]+/, ""));
|
|
32
31
|
}
|
|
33
32
|
const { methodName, file, lineNumber, column } = frame;
|
|
34
33
|
let s = " ";
|
|
@@ -60,14 +59,14 @@ function renderProps(error, options) {
|
|
|
60
59
|
if (options.omitProps) {
|
|
61
60
|
return "";
|
|
62
61
|
}
|
|
63
|
-
const ignore = [
|
|
62
|
+
const ignore = /* @__PURE__ */ new Set([
|
|
64
63
|
"name",
|
|
65
64
|
"message",
|
|
66
65
|
"frames",
|
|
67
66
|
"stack"
|
|
68
|
-
];
|
|
67
|
+
]);
|
|
69
68
|
const keys = Object.getOwnPropertyNames(error).filter((key) => {
|
|
70
|
-
return !ignore.
|
|
69
|
+
return !ignore.has(key);
|
|
71
70
|
});
|
|
72
71
|
if (!keys.length) {
|
|
73
72
|
return "";
|
|
@@ -84,12 +83,4 @@ function renderProps(error, options) {
|
|
|
84
83
|
}
|
|
85
84
|
__name(renderProps, "renderProps");
|
|
86
85
|
|
|
87
|
-
|
|
88
|
-
function enablePrettyStackTrace() {
|
|
89
|
-
process.prependOnceListener("uncaughtException", (e) => {
|
|
90
|
-
console.error(prettyStackTrace(e));
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
__name(enablePrettyStackTrace, "enablePrettyStackTrace");
|
|
94
|
-
|
|
95
|
-
export { enablePrettyStackTrace, prettyStackTrace };
|
|
86
|
+
export { prettyStackTrace };
|
package/package.json
CHANGED
|
@@ -1,36 +1,86 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/stacktrace",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
5
|
-
"type": "module",
|
|
6
|
-
"sideEffects": false,
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Beautiful, color-formatted stack trace output for Node.js errors with syntax-highlighted file paths and structured error properties.",
|
|
7
5
|
"keywords": [
|
|
8
|
-
"stacktrace"
|
|
6
|
+
"stacktrace",
|
|
7
|
+
"stack-trace",
|
|
8
|
+
"error",
|
|
9
|
+
"formatting",
|
|
10
|
+
"color",
|
|
11
|
+
"syntax-highlighting",
|
|
12
|
+
"node",
|
|
13
|
+
"utilities",
|
|
14
|
+
"typescript"
|
|
9
15
|
],
|
|
10
|
-
"exports": {
|
|
11
|
-
".": {
|
|
12
|
-
"types": "./index.d.ts",
|
|
13
|
-
"import": "./index.mjs",
|
|
14
|
-
"default": "./index.mjs"
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
16
|
"dependencies": {
|
|
18
17
|
"ansi-colors": "^4.1.3",
|
|
19
|
-
"
|
|
20
|
-
"
|
|
18
|
+
"upath": "^2.0.1",
|
|
19
|
+
"stacktrace-parser": "^0.1.11"
|
|
21
20
|
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"sideEffects": false,
|
|
22
23
|
"publishConfig": {
|
|
23
24
|
"access": "public"
|
|
24
25
|
},
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
26
|
+
"main": "./lib/index.cjs",
|
|
27
|
+
"module": "./lib/index.js",
|
|
28
|
+
"types": "./lib/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./lib/index.d.ts",
|
|
33
|
+
"default": "./lib/index.js"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./lib/index.d.cts",
|
|
37
|
+
"default": "./lib/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"./enablePrettyStackTrace": {
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./lib/enablePrettyStackTrace.d.ts",
|
|
43
|
+
"default": "./lib/enablePrettyStackTrace.js"
|
|
44
|
+
},
|
|
45
|
+
"require": {
|
|
46
|
+
"types": "./lib/enablePrettyStackTrace.d.cts",
|
|
47
|
+
"default": "./lib/enablePrettyStackTrace.cjs"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"./prettyStackTrace": {
|
|
51
|
+
"import": {
|
|
52
|
+
"types": "./lib/prettyStackTrace.d.ts",
|
|
53
|
+
"default": "./lib/prettyStackTrace.js"
|
|
54
|
+
},
|
|
55
|
+
"require": {
|
|
56
|
+
"types": "./lib/prettyStackTrace.d.cts",
|
|
57
|
+
"default": "./lib/prettyStackTrace.cjs"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
30
60
|
},
|
|
61
|
+
"files": [
|
|
62
|
+
"./lib/index.js",
|
|
63
|
+
"./lib/index.d.ts",
|
|
64
|
+
"./lib/index.cjs",
|
|
65
|
+
"./lib/index.d.cts",
|
|
66
|
+
"./lib/enablePrettyStackTrace.js",
|
|
67
|
+
"./lib/enablePrettyStackTrace.d.ts",
|
|
68
|
+
"./lib/enablePrettyStackTrace.cjs",
|
|
69
|
+
"./lib/enablePrettyStackTrace.d.cts",
|
|
70
|
+
"./lib/prettyStackTrace.js",
|
|
71
|
+
"./lib/prettyStackTrace.d.ts",
|
|
72
|
+
"./lib/prettyStackTrace.cjs",
|
|
73
|
+
"./lib/prettyStackTrace.d.cts"
|
|
74
|
+
],
|
|
31
75
|
"repository": {
|
|
32
76
|
"type": "git",
|
|
33
77
|
"url": "git+https://github.com/bemoje/mono.git",
|
|
34
78
|
"directory": "libs/stacktrace"
|
|
35
|
-
}
|
|
79
|
+
},
|
|
80
|
+
"author": {
|
|
81
|
+
"name": "Benjamin Møller Jensen",
|
|
82
|
+
"email": "bemoje@bemoje.net",
|
|
83
|
+
"url": "https://github.com/bemoje/"
|
|
84
|
+
},
|
|
85
|
+
"license": "MIT"
|
|
36
86
|
}
|