loggable-error 1.0.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/LICENSE +21 -0
- package/README.md +32 -0
- package/package.json +35 -0
- package/src/import.mjs +3 -0
- package/src/main.cjs +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Evan Kaufman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# loggable-error
|
|
2
|
+
|
|
3
|
+
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
|
+
|
|
5
|
+
```js
|
|
6
|
+
// supports cjs require
|
|
7
|
+
const stringify = require('loggable-error');
|
|
8
|
+
// or esm import
|
|
9
|
+
// import stringify from 'loggable-error';
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
throw new Error('testing one two three');
|
|
13
|
+
} catch(e) {
|
|
14
|
+
fs.writeFileSync(
|
|
15
|
+
'/home/jdoe/log.txt',
|
|
16
|
+
stringify(e)
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Open up said file and you'll find something along the lines of:
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
Error: testing one two three
|
|
25
|
+
at Object.<anonymous> (/home/jdoe/test.js:7:9)
|
|
26
|
+
at Module._compile (node:internal/modules/cjs/loader:1126:14)
|
|
27
|
+
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
|
|
28
|
+
at Module.load (node:internal/modules/cjs/loader:1004:32)
|
|
29
|
+
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
|
|
30
|
+
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
|
|
31
|
+
at node:internal/main/run_main_module:17:47
|
|
32
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "loggable-error",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Stringifies errors into a format similar to console.log",
|
|
5
|
+
"main": "./src/main.cjs",
|
|
6
|
+
"exports": {
|
|
7
|
+
"import": "./src/import.mjs",
|
|
8
|
+
"require": "./src/main.cjs"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "mocha ./tests/*.test.*js"
|
|
13
|
+
},
|
|
14
|
+
"author": "Evan Kaufman <evan@evanskaufman.com>",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"chai": "^5.1.1",
|
|
18
|
+
"mocha": "^10.7.3"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/EvanK/npm-loggable-error.git"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"error",
|
|
26
|
+
"exception",
|
|
27
|
+
"log",
|
|
28
|
+
"loggable",
|
|
29
|
+
"stringify"
|
|
30
|
+
],
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/EvanK/npm-loggable-error/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/EvanK/npm-loggable-error#readme"
|
|
35
|
+
}
|
package/src/import.mjs
ADDED
package/src/main.cjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts any Error instance to a string format similar to what console.log generates.
|
|
3
|
+
* Any other value will be cast to string.
|
|
4
|
+
*
|
|
5
|
+
* @param {*} err The error to stringify
|
|
6
|
+
* @param {number} depth For recursive calls on nested error objects, controls indentation
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
9
|
+
module.exports = function stringify (err, depth = 0) {
|
|
10
|
+
let collapsed = '';
|
|
11
|
+
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();
|
|
14
|
+
|
|
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);
|
|
18
|
+
}
|
|
19
|
+
collapsed += stack;
|
|
20
|
+
|
|
21
|
+
const props = Object.getOwnPropertyNames(err);
|
|
22
|
+
|
|
23
|
+
// skip these two ahead of time
|
|
24
|
+
for (const skipped of ['message','stack']) {
|
|
25
|
+
const propAt = props.indexOf(skipped);
|
|
26
|
+
if (propAt > -1) props.splice(propAt, 1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// only break into object notation if we have addtl props to dump
|
|
30
|
+
if (props.length) {
|
|
31
|
+
const dedent = ' '.repeat(depth);
|
|
32
|
+
const indent = ' '.repeat(depth + 2);
|
|
33
|
+
|
|
34
|
+
collapsed += ' {\n';
|
|
35
|
+
|
|
36
|
+
// loop and print each (indented) prop name
|
|
37
|
+
for (let property of props) {
|
|
38
|
+
collapsed += `${indent}[${property}]: `;
|
|
39
|
+
|
|
40
|
+
// if another error object, stringify it too
|
|
41
|
+
if (err[property] instanceof Error) {
|
|
42
|
+
collapsed += stringify(err[property], depth + 2);
|
|
43
|
+
}
|
|
44
|
+
// otherwise stringify as JSON
|
|
45
|
+
else {
|
|
46
|
+
collapsed += JSON.stringify(err[property]);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
collapsed += '\n';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
collapsed += `${dedent}}\n`;
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
collapsed += err;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return collapsed;
|
|
59
|
+
}
|
|
60
|
+
|