loggable-error 1.0.0 → 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.0",
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
@@ -1,22 +1,35 @@
1
+ const packageJson = require('../package.json');
2
+
1
3
  /**
2
4
  * Converts any Error instance to a string format similar to what console.log generates.
3
5
  * Any other value will be cast to string.
4
6
  *
5
7
  * @param {*} err The error to stringify
6
- * @param {number} depth For recursive calls on nested error objects, controls indentation
7
- * @returns {string}
8
+ * @param {object} [options]
9
+ * @param {number} [options.depth=0] Controls indentation of stack traces, properties and nested errors
10
+ * @param {boolean} [options.stack=true] Controls stack traces, displayed by default
11
+ * @returns {string} The stringified error
12
+ * @property {string} version
8
13
  */
9
- module.exports = function stringify (err, depth = 0) {
10
- let collapsed = '';
14
+ function stringify(err, options = {}) {
15
+ const depth = options.hasOwnProperty('depth') ? parseInt(options.depth, 10) : 0;
16
+ const stack = options.hasOwnProperty('stack') ? (!!options.stack) : true;
17
+
18
+ let collapsed = ' '.repeat(depth);
11
19
  if (err instanceof Error) {
12
- // add full stack trace if one exists, otherwise convert to string
13
- let stack = ( err?.stack ?? err.toString() ).replace(/^/gm, ' '.repeat(depth)).trim();
20
+ let body;
21
+ if (stack) {
22
+ // add full stack trace if one exists, otherwise convert to string
23
+ body = ( err?.stack ?? `${err}` ).replace(/^/gm, ' '.repeat(depth)).trim();
14
24
 
15
- // replace error name with class constructor as necessary
16
- if (stack.startsWith('Error: ') && err.constructor.name != 'Error') {
17
- stack = err.constructor.name + stack.substring(5);
25
+ // replace error name with class constructor as necessary
26
+ if (body.startsWith('Error: ') && err.constructor.name != 'Error') {
27
+ body = err.constructor.name + body.substring(5);
28
+ }
29
+ } else {
30
+ body = `${err}`;
18
31
  }
19
- collapsed += stack;
32
+ collapsed += body;
20
33
 
21
34
  const props = Object.getOwnPropertyNames(err);
22
35
 
@@ -39,7 +52,7 @@ module.exports = function stringify (err, depth = 0) {
39
52
 
40
53
  // if another error object, stringify it too
41
54
  if (err[property] instanceof Error) {
42
- collapsed += stringify(err[property], depth + 2);
55
+ collapsed += stringify(err[property], { depth: depth + 2, stack }).trimStart();
43
56
  }
44
57
  // otherwise stringify as JSON
45
58
  else {
@@ -57,4 +70,6 @@ module.exports = function stringify (err, depth = 0) {
57
70
 
58
71
  return collapsed;
59
72
  }
73
+ stringify.version = packageJson.version;
60
74
 
75
+ module.exports = stringify;