|
1 | | -# herro! [![NPM version][badge-npm]][link-npm][![npm downloads][badge-donwloads]][link-npm] |
2 | | - |
3 | | -[![Build][badge-build]][link-build] |
4 | | - |
5 | | -What if stack traces looked like this? |
6 | | - |
7 | | -```sh |
8 | | -Error: something broke |
9 | | - at Context.<anonymous> (./test/Herror.js:9:17) |
10 | | - at callFn (mocha@2.2.5/lib/runnable.js:266:21) |
11 | | - at Test.Runnable.run (mocha@2.2.5/lib/runnable.js:259:7) |
12 | | - at Runner.runTest (mocha@2.2.5/lib/runner.js:390:10) |
13 | | - at mocha@2.2.5/lib/runner.js:473:12 |
14 | | - at next (mocha@2.2.5/lib/runner.js:315:14) |
15 | | - at mocha@2.2.5/lib/runner.js:325:7 |
16 | | - at next (mocha@2.2.5/lib/runner.js:260:23) |
17 | | - at Immediate._onImmediate (mocha@2.2.5/lib/runner.js:292:5) |
18 | | - at processImmediate [as _immediateCallback] (timers.js:369:17) |
| 1 | +# herro |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/herro) |
| 4 | +[](https://www.npmjs.com/package/herro) |
| 5 | +[](https://github.com/stringparser/herro/actions/workflows/ci.yml) |
| 6 | + |
| 7 | +Humanized stack traces for the `Error` class: show `<module>@<version>` instead of `node_modules/<module>`, and add a footer with cwd, node version, and ISO date. |
| 8 | + |
| 9 | +**Example:** |
| 10 | + |
| 11 | +``` |
| 12 | +Herror: something broke |
| 13 | + at Context.<anonymous> (./test/Herror.test.ts:9:17) |
| 14 | + at callFn (jest@29.7.0/lib/runnable.js:266:21) |
| 15 | + at Test.Runnable.run (jest@29.7.0/lib/runner.js:259:7) |
| 16 | +... |
19 | 17 | -- |
20 | | -cwd /Users/stringparser/code/herro |
21 | | -node-v1.5.0 2015-06-14T16:09:05.544Z |
| 18 | +cwd /Users/you/code/your-app |
| 19 | +node-v20.10.0 2025-02-07T12:00:00.000Z |
22 | 20 | ``` |
23 | | -Changes paths that have |
24 | | - - The current working directory with `.` |
25 | | - - `node_modules/<module>` with `<module>@<version>` |
26 | 21 |
|
27 | | -Adds a footer with |
28 | | - - The current working directory |
29 | | - - An ISO date and node version from were `error` was thrown |
| 22 | +- Replaces **cwd** with `.` and **`node_modules/<module>`** with **`<module>@<version>`** |
| 23 | +- Appends **cwd**, **node version**, and **ISO date** to the stack |
30 | 24 |
|
31 | | -## api |
32 | | -```js |
33 | | -var herro = require('herro'); |
34 | | -``` |
| 25 | +--- |
35 | 26 |
|
36 | | -The `module.exports` two functions |
37 | | - - `herro.Herror` |
38 | | - - `herro.global` |
| 27 | +## Install |
39 | 28 |
|
40 | | -### Herror |
41 | | -```js |
42 | | -function Herror(String message) |
| 29 | +```bash |
| 30 | +npm install herro |
43 | 31 | ``` |
44 | | -`Error` class with paths on its stack trace as `<module>@<version>` |
45 | | -instead of `node_modules/<module>`. Inherits from the `Error` class. |
46 | 32 |
|
47 | | -_arguments_ |
48 | | -- `message` type string message for the error |
| 33 | +**Requires Node.js ≥20.** |
49 | 34 |
|
50 | | -_returns_ |
51 | | -- a new `error` instance |
| 35 | +--- |
52 | 36 |
|
53 | | -#### usage |
54 | | -```js |
55 | | -var Herror = require('herro').Herror; |
| 37 | +## API |
56 | 38 |
|
57 | | -if('something broke'){ |
58 | | - throw new Herror('oh, no, you did not!'); |
59 | | -} |
60 | | -``` |
| 39 | +Built in TypeScript; types are included. |
61 | 40 |
|
62 | | -### global |
63 | | -```js |
64 | | -function global([Boolean flag]) |
65 | | -``` |
| 41 | +### `Herror` |
66 | 42 |
|
67 | | -If `flag` is truthy or `undefined`, it will make all stack traces |
68 | | -have `<module>@<version>` instead of `node_modules/<module>`. If `flag` is falsy it will revert stacktraces to their default format. |
| 43 | +Error subclass whose instances get humanized stack traces. Use with `new` only. |
69 | 44 |
|
70 | | -#### usage |
71 | | -```js |
72 | | -var herro = require('herro'); |
| 45 | +```ts |
| 46 | +import { Herror } from 'herro'; |
73 | 47 |
|
74 | | -herro.global(); // make it global |
75 | | -herro.global(false); // go back to the normal stack format |
| 48 | +throw new Herror('something broke'); |
76 | 49 | ``` |
77 | 50 |
|
78 | | -## install |
| 51 | +### `global(flag?)` |
79 | 52 |
|
80 | | -With [npm](https://www.npmjs.com) |
| 53 | +When `flag` is truthy or omitted, all stack traces (including plain `Error`) use the humanized format. When `flag` is `false`, restores the default format. |
81 | 54 |
|
82 | | -``` |
83 | | -npm install herro |
| 55 | +```ts |
| 56 | +import { global } from 'herro'; |
| 57 | + |
| 58 | +global(); // enable humanized stacks globally |
| 59 | +global(false); // revert to default format |
84 | 60 | ``` |
85 | 61 |
|
86 | | -## test |
| 62 | +### Utilities |
87 | 63 |
|
88 | | -``` |
89 | | -herro (master) ✔ npm test |
90 | | - |
91 | | -herro |
92 | | - Herror |
93 | | - ✓ should format stack traces |
94 | | - ✓ should have an ISO date string |
95 | | - ✓ should have the current node version |
96 | | - ✓ should have the current working directory |
97 | | - npmize |
98 | | - ✓ CWD/node_modules/module/file.js into module@version |
99 | | - ✓ CWD/node_modules/m1/node_modules/m2/file.js into m1@version/m2@version2 |
100 | | - global |
101 | | - ✓ global(truthy || undefined) npmizes stacks |
102 | | - ✓ global(falsy) brings back default format |
103 | | - |
104 | | - |
105 | | -8 passing (21ms) |
106 | | -``` |
| 64 | +- **`npmize(stack: string): string`** – transform a stack string to `<module>@<version>` paths |
| 65 | +- **`formatStack(stack: string): string`** – npmize and append cwd / node version / date footer |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## Scripts |
107 | 70 |
|
108 | | -## license |
| 71 | +| Command | Description | |
| 72 | +|-------------------|----------------------------| |
| 73 | +| `npm run dist` | Build TypeScript to `dist/` | |
| 74 | +| `npm run lint` | Run ESLint | |
| 75 | +| `npm test` | Run Jest + verify-publish | |
| 76 | +| `npm run clean` | Remove verify-publish artifacts and `*.tgz` | |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## License |
109 | 81 |
|
110 | 82 | The MIT License (MIT) |
111 | 83 |
|
112 | | -Copyright (c) 2014-present Javier Carrillo |
| 84 | +Copyright (c) 2017-present Javier Carrillo |
113 | 85 |
|
114 | 86 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
115 | 87 |
|
116 | 88 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
117 | 89 |
|
118 | 90 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
119 | | - |
120 | | -<!-- links, etc --> |
121 | | - |
122 | | -[link-npm]: http://www.npmjs.org/package/herro |
123 | | -[link-build]: https://travis-ci.org/stringparser/herro/builds |
124 | | - |
125 | | -[badge-npm]: http://img.shields.io/npm/v/herro.svg?style=flat-square |
126 | | -[badge-build]: http://img.shields.io/travis/stringparser/herro/master.svg?style=flat-square |
127 | | -[badge-donwloads]: http://img.shields.io/npm/dm/herro.svg?style=flat-square |
0 commit comments