loggable-error 1.0.1 → 1.0.2

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.
@@ -0,0 +1,27 @@
1
+ name: Unit tests in CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ "main" ]
6
+ pull_request:
7
+ branches: [ "main" ]
8
+
9
+ jobs:
10
+ build:
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ strategy:
15
+ matrix:
16
+ node-version: [18.x, 20.x, 22.x]
17
+
18
+ steps:
19
+ - uses: actions/checkout@v4
20
+ - name: Use Node.js ${{ matrix.node-version }}
21
+ uses: actions/setup-node@v4
22
+ with:
23
+ node-version: ${{ matrix.node-version }}
24
+ cache: 'npm'
25
+ - run: npm ci
26
+ - run: npm run build --if-present
27
+ - run: npm test
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # loggable-error
2
2
 
3
+ ![ci status](https://github.com/EvanK/npm-loggable-error/actions/workflows/ci.yml/badge.svg)
4
+
3
5
  In those times when you need to log an error to somewhere other than standard output, this simple module stringifies Error objects in a format akin to `console.log`:
4
6
 
5
7
  ```js
@@ -30,3 +32,26 @@ Error: testing one two three
30
32
  at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
31
33
  at node:internal/main/run_main_module:17:47
32
34
  ```
35
+
36
+ ## Stringification Options
37
+
38
+ Our exported function accepts a second `options` argument, in case you want to control whether stack traces are displayed or the amount of starting indentation:
39
+
40
+ ```js
41
+ stringify(e, { stack: false });
42
+ // => 'Error: testing one two three'
43
+
44
+ stringify(e, { depth: 8 });
45
+ /* =>
46
+ ' Error: testing one two three\n' +
47
+ ' at Object.<anonymous> (/home/jdoe/test.js:7:9)\n' +
48
+ ' at Module._compile (node:internal/modules/cjs/loader:1546:14)\n' +
49
+ ' at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)\n' +
50
+ ' at Module.load (node:internal/modules/cjs/loader:1317:32)\n' +
51
+ ' at Module._load (node:internal/modules/cjs/loader:1127:12)\n' +
52
+ ' at TracingChannel.traceSync (node:diagnostics_channel:315:14)\n' +
53
+ ' at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)\n' +
54
+ ' at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:166:5)\n' +
55
+ ' at node:internal/main/run_main_module:30:49'
56
+ */
57
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "loggable-error",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Stringifies errors into a format similar to console.log",
5
5
  "main": "./src/main.cjs",
6
6
  "exports": {
package/src/main.cjs CHANGED
@@ -15,7 +15,7 @@ function stringify(err, options = {}) {
15
15
  const depth = options.hasOwnProperty('depth') ? parseInt(options.depth, 10) : 0;
16
16
  const stack = options.hasOwnProperty('stack') ? (!!options.stack) : true;
17
17
 
18
- let collapsed = '';
18
+ let collapsed = ' '.repeat(depth);
19
19
  if (err instanceof Error) {
20
20
  let body;
21
21
  if (stack) {
@@ -52,7 +52,7 @@ function stringify(err, options = {}) {
52
52
 
53
53
  // if another error object, stringify it too
54
54
  if (err[property] instanceof Error) {
55
- collapsed += stringify(err[property], { depth: depth + 2, stack });
55
+ collapsed += stringify(err[property], { depth: depth + 2, stack }).trimStart();
56
56
  }
57
57
  // otherwise stringify as JSON
58
58
  else {