loggable-error 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/main.cjs +25 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "loggable-error",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
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) {
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
+
10
18
  let collapsed = '';
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 });
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;