sweet-error 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.
- package/LICENSE +21 -0
- package/README.md +115 -0
- package/dist/SweetError.js +252 -0
- package/dist/SweetError.js.map +1 -0
- package/dist/SweetError.mjs +250 -0
- package/dist/SweetError.mjs.map +1 -0
- package/dist/types/SweetError.d.ts +64 -0
- package/dist/types/utils/colorize.d.ts +7 -0
- package/dist/types/utils/format.d.ts +7 -0
- package/dist/types/utils/indent.d.ts +8 -0
- package/dist/types/utils/logger.d.ts +18 -0
- package/dist/types/utils/wrap.d.ts +12 -0
- package/package.json +44 -0
- package/sweet-error.png +0 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 𝖊𝖈𝖍𝖔
|
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,115 @@
|
|
1
|
+
# SweetError
|
2
|
+
|
3
|
+
Makes nodeJs Error cleaner, more readable and better formatted
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
#### NodeJs
|
8
|
+
|
9
|
+
```sh
|
10
|
+
npm install sweet-error
|
11
|
+
```
|
12
|
+
|
13
|
+
## Example
|
14
|
+
|
15
|
+
```js
|
16
|
+
// cjs
|
17
|
+
const { SweetError } = require('sweet-error');
|
18
|
+
// mjs
|
19
|
+
import { SweetError } from 'sweet-error';
|
20
|
+
|
21
|
+
new SweetError(
|
22
|
+
[
|
23
|
+
'Invalid password',
|
24
|
+
'A password should ... for example:',
|
25
|
+
['Abc242$3231@', '323CR#@#IY', 'sgfgsfg%$$5#22323'],
|
26
|
+
],
|
27
|
+
{
|
28
|
+
name: 'PwdValidator',
|
29
|
+
code: 'PwdValidator.invalidPwd',
|
30
|
+
exitCode: 1,
|
31
|
+
}
|
32
|
+
);
|
33
|
+
```
|
34
|
+
|
35
|
+
<br />
|
36
|
+
|
37
|
+
<div align='center'>
|
38
|
+
<img src="./sweet-error.png" style='border-radius: 10px'></img>
|
39
|
+
</div>
|
40
|
+
|
41
|
+
## Api
|
42
|
+
|
43
|
+
```js
|
44
|
+
const error = new SweetError(messages, [options]);
|
45
|
+
```
|
46
|
+
|
47
|
+
### messages
|
48
|
+
|
49
|
+
**required**
|
50
|
+
Type: `any[]`
|
51
|
+
An array of messages, where each message can be of any data type, and each index in the array represents a line.
|
52
|
+
|
53
|
+
### options
|
54
|
+
|
55
|
+
- **name**
|
56
|
+
Type: `string`
|
57
|
+
The string represents the instance name e.g: `SomeError`
|
58
|
+
|
59
|
+
- **code**
|
60
|
+
Type: `string`
|
61
|
+
The string represents the instance code e.g: `SomeError.SomethingWrong`
|
62
|
+
|
63
|
+
- **exitCode**
|
64
|
+
Type: `string | number`
|
65
|
+
The code to exit the `node.js` process with
|
66
|
+
|
67
|
+
- **colorize**
|
68
|
+
Type: `boolean`
|
69
|
+
Default: `true`
|
70
|
+
Enables or disables text colorization
|
71
|
+
|
72
|
+
- **logger**
|
73
|
+
Type: `function`
|
74
|
+
The function that controls how the Error appears and how its parts are arranged, with some helpful utils that the `SweetError` uses internally
|
75
|
+
|
76
|
+
### error.messages
|
77
|
+
|
78
|
+
Type: `any[]`
|
79
|
+
Returns the specified messages array that is used to initialize the instance
|
80
|
+
|
81
|
+
### error.options
|
82
|
+
|
83
|
+
Type: `SweetErrorOptions | undefined`
|
84
|
+
Returns the options object that the instance initialized with
|
85
|
+
|
86
|
+
### error.name
|
87
|
+
|
88
|
+
Type: `string | undefined`
|
89
|
+
Returns the instance name
|
90
|
+
|
91
|
+
### error.code
|
92
|
+
|
93
|
+
Type: `string | undefined`
|
94
|
+
Returns the instance code of the error
|
95
|
+
|
96
|
+
### error.exitCode
|
97
|
+
|
98
|
+
Type: `string | number | undefined`
|
99
|
+
Returns the instance exit code that was used to exit the runtime.
|
100
|
+
|
101
|
+
### error.stack
|
102
|
+
|
103
|
+
Type: `any[]`
|
104
|
+
Returns an array of objects representing the captured instance stack.
|
105
|
+
|
106
|
+
### error.func
|
107
|
+
|
108
|
+
Type: `Function[]`
|
109
|
+
Returns an array of utility functions used internally to customize error output. These functions can also be destructured from this within the logger function in the instance's options object.
|
110
|
+
|
111
|
+
<h4 align='center'>Please Hit the ⭐ button if you found this useful</h4>
|
112
|
+
|
113
|
+
## License
|
114
|
+
|
115
|
+
[MIT License ©](./LICENSE)
|
@@ -0,0 +1,252 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var StackUtils = require('stack-utils');
|
4
|
+
var chalk = require('chalk');
|
5
|
+
var json5 = require('json5');
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Colorize string sentences
|
9
|
+
*
|
10
|
+
* @param {string} sentence string to colorize
|
11
|
+
* @returns {string}
|
12
|
+
*/
|
13
|
+
function colorize(sentence) {
|
14
|
+
const colors = [
|
15
|
+
'red',
|
16
|
+
'green',
|
17
|
+
'redBright',
|
18
|
+
'yellow',
|
19
|
+
'blue',
|
20
|
+
'greenBright',
|
21
|
+
'magenta',
|
22
|
+
'cyan',
|
23
|
+
'yellowBright',
|
24
|
+
'white',
|
25
|
+
'blueBright',
|
26
|
+
'magentaBright',
|
27
|
+
'cyanBright',
|
28
|
+
'whiteBright',
|
29
|
+
];
|
30
|
+
const parts = sentence.split(/(\s+)/);
|
31
|
+
return parts
|
32
|
+
.map((part) => {
|
33
|
+
if (/\S/.test(part)) {
|
34
|
+
return chalk[colors[Math.floor(Math.random() * colors.length)]](part);
|
35
|
+
}
|
36
|
+
return part;
|
37
|
+
})
|
38
|
+
.join('');
|
39
|
+
}
|
40
|
+
|
41
|
+
/**
|
42
|
+
* json5 stringifier
|
43
|
+
*
|
44
|
+
* @param {object} obj
|
45
|
+
* @returns {string}
|
46
|
+
*/
|
47
|
+
function format(object) {
|
48
|
+
return json5.stringify(object, null, 2);
|
49
|
+
}
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Add string indentation
|
53
|
+
*
|
54
|
+
* @param {string} string
|
55
|
+
* @param {number} padding indentation lavel
|
56
|
+
* @returns {string}
|
57
|
+
*/
|
58
|
+
function indent(string, padding) {
|
59
|
+
return string
|
60
|
+
.trim()
|
61
|
+
.split('\n')
|
62
|
+
.map((line) => {
|
63
|
+
return `${' '.repeat(padding || 0)}${line}`;
|
64
|
+
})
|
65
|
+
.join('\n');
|
66
|
+
}
|
67
|
+
|
68
|
+
/**
|
69
|
+
* Controls how lines should wrap
|
70
|
+
*
|
71
|
+
* @param {string} text
|
72
|
+
* @param {number} width
|
73
|
+
* @returns {string}
|
74
|
+
*/
|
75
|
+
function wrap(
|
76
|
+
/** The string to wrap */
|
77
|
+
text,
|
78
|
+
/** Line width to break the words at */
|
79
|
+
width = Math.min(Math.max(20, process.stdout.columns) || 80, 80)) {
|
80
|
+
let result = '';
|
81
|
+
let lineLength = 0;
|
82
|
+
for (const word of text.split(' ')) {
|
83
|
+
if (lineLength + word.length > width) {
|
84
|
+
result += '\n' + word;
|
85
|
+
lineLength = word.length;
|
86
|
+
}
|
87
|
+
else {
|
88
|
+
if (lineLength > 0) {
|
89
|
+
result += ' ';
|
90
|
+
lineLength++;
|
91
|
+
}
|
92
|
+
result += word;
|
93
|
+
lineLength += word.length;
|
94
|
+
}
|
95
|
+
}
|
96
|
+
return result;
|
97
|
+
}
|
98
|
+
|
99
|
+
/**
|
100
|
+
* The function that controls how the Error appears and how its parts are arranged
|
101
|
+
*
|
102
|
+
* @memberof SweetError
|
103
|
+
* @example ```js
|
104
|
+
* function logger() {
|
105
|
+
* // you have access to all the properties of the SweetError to customize the way it appears
|
106
|
+
* const { messages, name, code, stack, func } = this;
|
107
|
+
* // also some utils may be helpful
|
108
|
+
* const { format, indent, wrap } = func;
|
109
|
+
* }
|
110
|
+
*
|
111
|
+
* const opts = { logger };
|
112
|
+
*
|
113
|
+
* new SweetError([], opts);
|
114
|
+
* ```
|
115
|
+
*/
|
116
|
+
function logger() {
|
117
|
+
var _a, _b, _c;
|
118
|
+
logName(this);
|
119
|
+
for (let i = 0; i < this.messages.length; i++) {
|
120
|
+
const message = this.messages[i];
|
121
|
+
const lastIndex = this.messages.length - 1;
|
122
|
+
if (typeof message === 'string') {
|
123
|
+
if (this === null || this === void 0 ? void 0 : this.name) {
|
124
|
+
if (((_a = this === null || this === void 0 ? void 0 : this.options) === null || _a === void 0 ? void 0 : _a.colorize) === true) {
|
125
|
+
console.log(colorize(indent(wrap(message), 2)));
|
126
|
+
}
|
127
|
+
else {
|
128
|
+
console.log(indent(wrap(message), 2));
|
129
|
+
}
|
130
|
+
}
|
131
|
+
else {
|
132
|
+
if (((_b = this === null || this === void 0 ? void 0 : this.options) === null || _b === void 0 ? void 0 : _b.colorize) === true) {
|
133
|
+
console.log(colorize(wrap(message)));
|
134
|
+
}
|
135
|
+
else {
|
136
|
+
console.log(wrap(message));
|
137
|
+
}
|
138
|
+
}
|
139
|
+
}
|
140
|
+
else if (typeof message === 'object') {
|
141
|
+
if (i != 0) {
|
142
|
+
console.log('');
|
143
|
+
}
|
144
|
+
console.log(indent(format(message), 2));
|
145
|
+
if (i != lastIndex) {
|
146
|
+
console.log('');
|
147
|
+
}
|
148
|
+
}
|
149
|
+
else {
|
150
|
+
console.log(' ' + message);
|
151
|
+
}
|
152
|
+
}
|
153
|
+
console.log('');
|
154
|
+
logStack(this);
|
155
|
+
console.log('');
|
156
|
+
logCode(this);
|
157
|
+
if (this === null || this === void 0 ? void 0 : this.exitCode) {
|
158
|
+
console.log('');
|
159
|
+
((_c = this === null || this === void 0 ? void 0 : this.options) === null || _c === void 0 ? void 0 : _c.colorize)
|
160
|
+
? console.log('🟡', chalk.yellow('Exit:'), this.exitCode)
|
161
|
+
: console.log('🟡', 'Exit:', this.exitCode);
|
162
|
+
}
|
163
|
+
}
|
164
|
+
/**
|
165
|
+
* Log the SweetError name
|
166
|
+
*
|
167
|
+
* @memberof logger
|
168
|
+
* @param {SweetError} error
|
169
|
+
*/
|
170
|
+
function logName(error) {
|
171
|
+
if (!(error === null || error === void 0 ? void 0 : error.name))
|
172
|
+
return;
|
173
|
+
return console.log('🔴', chalk.red.bold.underline(error.name));
|
174
|
+
}
|
175
|
+
/**
|
176
|
+
* Log the SweetError code
|
177
|
+
* @memberof logger
|
178
|
+
* @param {SweetError} error
|
179
|
+
*/
|
180
|
+
function logCode(error) {
|
181
|
+
var _a;
|
182
|
+
if (!(error === null || error === void 0 ? void 0 : error.code))
|
183
|
+
return;
|
184
|
+
return ((_a = error === null || error === void 0 ? void 0 : error.options) === null || _a === void 0 ? void 0 : _a.colorize)
|
185
|
+
? console.log(indent(chalk.gray.underline(`${error.code}`), 2))
|
186
|
+
: console.log(indent(chalk.underline(`${error.code}`), 2));
|
187
|
+
}
|
188
|
+
/**
|
189
|
+
* Log the SweetError stack trace lines
|
190
|
+
* @memberof logger
|
191
|
+
* @param {SweetError} error
|
192
|
+
*/
|
193
|
+
function logStack(error) {
|
194
|
+
error.stack.shift();
|
195
|
+
error.stack.forEach((object) => {
|
196
|
+
if (!object)
|
197
|
+
return;
|
198
|
+
let stackLine = '';
|
199
|
+
Object.keys(object).forEach((key) => {
|
200
|
+
var _a;
|
201
|
+
if (((_a = error === null || error === void 0 ? void 0 : error.options) === null || _a === void 0 ? void 0 : _a.colorize) === true) {
|
202
|
+
stackLine += `${chalk.bold(key)}: ${colorize(chalk.italic(object[key]))} `;
|
203
|
+
}
|
204
|
+
else {
|
205
|
+
stackLine += `${chalk.bold(key)}: ${chalk.italic(object[key])} `;
|
206
|
+
}
|
207
|
+
});
|
208
|
+
return console.log(indent('→ ' + stackLine.trim(), 2));
|
209
|
+
});
|
210
|
+
}
|
211
|
+
|
212
|
+
const stackUtils = new StackUtils({ cwd: process.cwd() });
|
213
|
+
/**
|
214
|
+
* Initializ a new `SweetError` instance
|
215
|
+
*/
|
216
|
+
class SweetError {
|
217
|
+
constructor(messages, options) {
|
218
|
+
if (!Array.isArray(messages)) {
|
219
|
+
throw new Error('SweetError.messages is not array');
|
220
|
+
}
|
221
|
+
this.messages = messages;
|
222
|
+
if (Object.prototype.toString.call(options) === '[object Object]') {
|
223
|
+
this.options = options;
|
224
|
+
if (!('logger' in this.options)) {
|
225
|
+
this.options.logger = logger;
|
226
|
+
}
|
227
|
+
if (!('colorize' in this.options)) {
|
228
|
+
this.options.colorize = true;
|
229
|
+
}
|
230
|
+
Object.keys(options).forEach(opt => {
|
231
|
+
if (options[opt] && opt != 'colorize' && opt != 'logger') {
|
232
|
+
this[opt] = options[opt];
|
233
|
+
}
|
234
|
+
});
|
235
|
+
}
|
236
|
+
else {
|
237
|
+
this.options = { logger, colorize: true };
|
238
|
+
}
|
239
|
+
this.stack = stackUtils
|
240
|
+
.captureString()
|
241
|
+
.split('\n')
|
242
|
+
.map((line) => {
|
243
|
+
return stackUtils.parseLine(line);
|
244
|
+
});
|
245
|
+
this.func = { colorize, format, indent, wrap };
|
246
|
+
this.options.logger.call(this);
|
247
|
+
return process.exit((this === null || this === void 0 ? void 0 : this.exitCode) || 0);
|
248
|
+
}
|
249
|
+
}
|
250
|
+
|
251
|
+
exports.SweetError = SweetError;
|
252
|
+
//# sourceMappingURL=SweetError.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"SweetError.js","sources":["../src/utils/colorize.ts","../src/utils/format.ts","../src/utils/indent.ts","../src/utils/wrap.ts","../src/utils/logger.ts","../src/SweetError.ts"],"sourcesContent":[null,null,null,null,null,null],"names":[],"mappings":";;;;;;AAEA;;;;;AAKG;AACG,SAAU,QAAQ,CAAC,QAAgB,EAAA;AACvC,IAAA,MAAM,MAAM,GAAa;QACvB,KAAK;QACL,OAAO;QACP,WAAW;QACX,QAAQ;QACR,MAAM;QACN,aAAa;QACb,SAAS;QACT,MAAM;QACN,cAAc;QACd,OAAO;QACP,YAAY;QACZ,eAAe;QACf,YAAY;QACZ,aAAa;KACd;IAED,MAAM,KAAK,GAAa,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC;AAE/C,IAAA,OAAO;AACJ,SAAA,GAAG,CAAC,CAAC,IAAY,KAAI;AACpB,QAAA,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YACnB,OAAO,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;;AAGvE,QAAA,OAAO,IAAI;AACb,KAAC;SACA,IAAI,CAAC,EAAE,CAAC;AACb;;ACnCA;;;;;AAKG;AACG,SAAU,MAAM,CAAC,MAAW,EAAA;IAChC,OAAO,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;AACzC;;ACVA;;;;;;AAMG;AACa,SAAA,MAAM,CAAC,MAAc,EAAE,OAAgB,EAAA;AACr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AAC;;aACG;YACL,IAAI,CAAC,OAAO,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;;QAG3C,IAAI,CAAC,KAAK,GAAG;AACV,aAAA,aAAa;aACb,KAAK,CAAC,IAAI;AACV,aAAA,GAAG,CAAC,CAAC,IAAY,KAAI;AACpB,YAAA,OAAO,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC;AACnC,SAAC,CAAC;AAEJ,QAAA,IAAI,CAAC,IAAI,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE;QAE9C,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;AAE9B,QAAA,OAAO,OAAO,CAAC,IAAI,CAAC,CAAA,IAAI,KAAA,IAAA,IAAJ,IAAI,KAAA,MAAA,GAAA,MAAA,GAAJ,IAAI,CAAE,QAAQ,KAAI,CAAC,CAAC;;AAE3C;;;;"}
|
@@ -0,0 +1,250 @@
|
|
1
|
+
import StackUtils from 'stack-utils';
|
2
|
+
import chalk from 'chalk';
|
3
|
+
import json5 from 'json5';
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Colorize string sentences
|
7
|
+
*
|
8
|
+
* @param {string} sentence string to colorize
|
9
|
+
* @returns {string}
|
10
|
+
*/
|
11
|
+
function colorize(sentence) {
|
12
|
+
const colors = [
|
13
|
+
'red',
|
14
|
+
'green',
|
15
|
+
'redBright',
|
16
|
+
'yellow',
|
17
|
+
'blue',
|
18
|
+
'greenBright',
|
19
|
+
'magenta',
|
20
|
+
'cyan',
|
21
|
+
'yellowBright',
|
22
|
+
'white',
|
23
|
+
'blueBright',
|
24
|
+
'magentaBright',
|
25
|
+
'cyanBright',
|
26
|
+
'whiteBright',
|
27
|
+
];
|
28
|
+
const parts = sentence.split(/(\s+)/);
|
29
|
+
return parts
|
30
|
+
.map((part) => {
|
31
|
+
if (/\S/.test(part)) {
|
32
|
+
return chalk[colors[Math.floor(Math.random() * colors.length)]](part);
|
33
|
+
}
|
34
|
+
return part;
|
35
|
+
})
|
36
|
+
.join('');
|
37
|
+
}
|
38
|
+
|
39
|
+
/**
|
40
|
+
* json5 stringifier
|
41
|
+
*
|
42
|
+
* @param {object} obj
|
43
|
+
* @returns {string}
|
44
|
+
*/
|
45
|
+
function format(object) {
|
46
|
+
return json5.stringify(object, null, 2);
|
47
|
+
}
|
48
|
+
|
49
|
+
/**
|
50
|
+
* Add string indentation
|
51
|
+
*
|
52
|
+
* @param {string} string
|
53
|
+
* @param {number} padding indentation lavel
|
54
|
+
* @returns {string}
|
55
|
+
*/
|
56
|
+
function indent(string, padding) {
|
57
|
+
return string
|
58
|
+
.trim()
|
59
|
+
.split('\n')
|
60
|
+
.map((line) => {
|
61
|
+
return `${' '.repeat(padding || 0)}${line}`;
|
62
|
+
})
|
63
|
+
.join('\n');
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* Controls how lines should wrap
|
68
|
+
*
|
69
|
+
* @param {string} text
|
70
|
+
* @param {number} width
|
71
|
+
* @returns {string}
|
72
|
+
*/
|
73
|
+
function wrap(
|
74
|
+
/** The string to wrap */
|
75
|
+
text,
|
76
|
+
/** Line width to break the words at */
|
77
|
+
width = Math.min(Math.max(20, process.stdout.columns) || 80, 80)) {
|
78
|
+
let result = '';
|
79
|
+
let lineLength = 0;
|
80
|
+
for (const word of text.split(' ')) {
|
81
|
+
if (lineLength + word.length > width) {
|
82
|
+
result += '\n' + word;
|
83
|
+
lineLength = word.length;
|
84
|
+
}
|
85
|
+
else {
|
86
|
+
if (lineLength > 0) {
|
87
|
+
result += ' ';
|
88
|
+
lineLength++;
|
89
|
+
}
|
90
|
+
result += word;
|
91
|
+
lineLength += word.length;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
return result;
|
95
|
+
}
|
96
|
+
|
97
|
+
/**
|
98
|
+
* The function that controls how the Error appears and how its parts are arranged
|
99
|
+
*
|
100
|
+
* @memberof SweetError
|
101
|
+
* @example ```js
|
102
|
+
* function logger() {
|
103
|
+
* // you have access to all the properties of the SweetError to customize the way it appears
|
104
|
+
* const { messages, name, code, stack, func } = this;
|
105
|
+
* // also some utils may be helpful
|
106
|
+
* const { format, indent, wrap } = func;
|
107
|
+
* }
|
108
|
+
*
|
109
|
+
* const opts = { logger };
|
110
|
+
*
|
111
|
+
* new SweetError([], opts);
|
112
|
+
* ```
|
113
|
+
*/
|
114
|
+
function logger() {
|
115
|
+
var _a, _b, _c;
|
116
|
+
logName(this);
|
117
|
+
for (let i = 0; i < this.messages.length; i++) {
|
118
|
+
const message = this.messages[i];
|
119
|
+
const lastIndex = this.messages.length - 1;
|
120
|
+
if (typeof message === 'string') {
|
121
|
+
if (this === null || this === void 0 ? void 0 : this.name) {
|
122
|
+
if (((_a = this === null || this === void 0 ? void 0 : this.options) === null || _a === void 0 ? void 0 : _a.colorize) === true) {
|
123
|
+
console.log(colorize(indent(wrap(message), 2)));
|
124
|
+
}
|
125
|
+
else {
|
126
|
+
console.log(indent(wrap(message), 2));
|
127
|
+
}
|
128
|
+
}
|
129
|
+
else {
|
130
|
+
if (((_b = this === null || this === void 0 ? void 0 : this.options) === null || _b === void 0 ? void 0 : _b.colorize) === true) {
|
131
|
+
console.log(colorize(wrap(message)));
|
132
|
+
}
|
133
|
+
else {
|
134
|
+
console.log(wrap(message));
|
135
|
+
}
|
136
|
+
}
|
137
|
+
}
|
138
|
+
else if (typeof message === 'object') {
|
139
|
+
if (i != 0) {
|
140
|
+
console.log('');
|
141
|
+
}
|
142
|
+
console.log(indent(format(message), 2));
|
143
|
+
if (i != lastIndex) {
|
144
|
+
console.log('');
|
145
|
+
}
|
146
|
+
}
|
147
|
+
else {
|
148
|
+
console.log(' ' + message);
|
149
|
+
}
|
150
|
+
}
|
151
|
+
console.log('');
|
152
|
+
logStack(this);
|
153
|
+
console.log('');
|
154
|
+
logCode(this);
|
155
|
+
if (this === null || this === void 0 ? void 0 : this.exitCode) {
|
156
|
+
console.log('');
|
157
|
+
((_c = this === null || this === void 0 ? void 0 : this.options) === null || _c === void 0 ? void 0 : _c.colorize)
|
158
|
+
? console.log('🟡', chalk.yellow('Exit:'), this.exitCode)
|
159
|
+
: console.log('🟡', 'Exit:', this.exitCode);
|
160
|
+
}
|
161
|
+
}
|
162
|
+
/**
|
163
|
+
* Log the SweetError name
|
164
|
+
*
|
165
|
+
* @memberof logger
|
166
|
+
* @param {SweetError} error
|
167
|
+
*/
|
168
|
+
function logName(error) {
|
169
|
+
if (!(error === null || error === void 0 ? void 0 : error.name))
|
170
|
+
return;
|
171
|
+
return console.log('🔴', chalk.red.bold.underline(error.name));
|
172
|
+
}
|
173
|
+
/**
|
174
|
+
* Log the SweetError code
|
175
|
+
* @memberof logger
|
176
|
+
* @param {SweetError} error
|
177
|
+
*/
|
178
|
+
function logCode(error) {
|
179
|
+
var _a;
|
180
|
+
if (!(error === null || error === void 0 ? void 0 : error.code))
|
181
|
+
return;
|
182
|
+
return ((_a = error === null || error === void 0 ? void 0 : error.options) === null || _a === void 0 ? void 0 : _a.colorize)
|
183
|
+
? console.log(indent(chalk.gray.underline(`${error.code}`), 2))
|
184
|
+
: console.log(indent(chalk.underline(`${error.code}`), 2));
|
185
|
+
}
|
186
|
+
/**
|
187
|
+
* Log the SweetError stack trace lines
|
188
|
+
* @memberof logger
|
189
|
+
* @param {SweetError} error
|
190
|
+
*/
|
191
|
+
function logStack(error) {
|
192
|
+
error.stack.shift();
|
193
|
+
error.stack.forEach((object) => {
|
194
|
+
if (!object)
|
195
|
+
return;
|
196
|
+
let stackLine = '';
|
197
|
+
Object.keys(object).forEach((key) => {
|
198
|
+
var _a;
|
199
|
+
if (((_a = error === null || error === void 0 ? void 0 : error.options) === null || _a === void 0 ? void 0 : _a.colorize) === true) {
|
200
|
+
stackLine += `${chalk.bold(key)}: ${colorize(chalk.italic(object[key]))} `;
|
201
|
+
}
|
202
|
+
else {
|
203
|
+
stackLine += `${chalk.bold(key)}: ${chalk.italic(object[key])} `;
|
204
|
+
}
|
205
|
+
});
|
206
|
+
return console.log(indent('→ ' + stackLine.trim(), 2));
|
207
|
+
});
|
208
|
+
}
|
209
|
+
|
210
|
+
const stackUtils = new StackUtils({ cwd: process.cwd() });
|
211
|
+
/**
|
212
|
+
* Initializ a new `SweetError` instance
|
213
|
+
*/
|
214
|
+
class SweetError {
|
215
|
+
constructor(messages, options) {
|
216
|
+
if (!Array.isArray(messages)) {
|
217
|
+
throw new Error('SweetError.messages is not array');
|
218
|
+
}
|
219
|
+
this.messages = messages;
|
220
|
+
if (Object.prototype.toString.call(options) === '[object Object]') {
|
221
|
+
this.options = options;
|
222
|
+
if (!('logger' in this.options)) {
|
223
|
+
this.options.logger = logger;
|
224
|
+
}
|
225
|
+
if (!('colorize' in this.options)) {
|
226
|
+
this.options.colorize = true;
|
227
|
+
}
|
228
|
+
Object.keys(options).forEach(opt => {
|
229
|
+
if (options[opt] && opt != 'colorize' && opt != 'logger') {
|
230
|
+
this[opt] = options[opt];
|
231
|
+
}
|
232
|
+
});
|
233
|
+
}
|
234
|
+
else {
|
235
|
+
this.options = { logger, colorize: true };
|
236
|
+
}
|
237
|
+
this.stack = stackUtils
|
238
|
+
.captureString()
|
239
|
+
.split('\n')
|
240
|
+
.map((line) => {
|
241
|
+
return stackUtils.parseLine(line);
|
242
|
+
});
|
243
|
+
this.func = { colorize, format, indent, wrap };
|
244
|
+
this.options.logger.call(this);
|
245
|
+
return process.exit((this === null || this === void 0 ? void 0 : this.exitCode) || 0);
|
246
|
+
}
|
247
|
+
}
|
248
|
+
|
249
|
+
export { SweetError };
|
250
|
+
//# sourceMappingURL=SweetError.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"SweetError.mjs","sources":["../src/utils/colorize.ts","../src/utils/format.ts","../src/utils/indent.ts","../src/utils/wrap.ts","../src/utils/logger.ts","../src/SweetError.ts"],"sourcesContent":[null,null,null,null,null,null],"names":[],"mappings":";;;;AAEA;;;;;AAKG;AACG,SAAU,QAAQ,CAAC,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;AAE9B,QAAA,OAAO,OAAO,CAAC,IAAI,CAAC,CAAA,IAAI,KAAA,IAAA,IAAJ,IAAI,KAAA,MAAA,GAAA,MAAA,GAAJ,IAAI,CAAE,QAAQ,KAAI,CAAC,CAAC;;AAE3C;;;;"}
|
@@ -0,0 +1,64 @@
|
|
1
|
+
interface SweetErrorOptions {
|
2
|
+
/** The instance name e.g `SomeError` */
|
3
|
+
name?: string;
|
4
|
+
/** The instance code e.g `SomeError.SomethingWrong` */
|
5
|
+
code?: string;
|
6
|
+
/** The code to exit the `node.js` process with */
|
7
|
+
exitCode?: string | number;
|
8
|
+
/** Enables or disables text colorization - (default is true) */
|
9
|
+
colorize?: boolean;
|
10
|
+
/** Controls how should `SweetError` output looks and organized */
|
11
|
+
logger?: () => void;
|
12
|
+
}
|
13
|
+
/**
|
14
|
+
* Initializ a new `SweetError` instance
|
15
|
+
*/
|
16
|
+
export declare class SweetError {
|
17
|
+
/** An array of messages, each index represents a line */
|
18
|
+
messages: any[];
|
19
|
+
/** SweetError options object */
|
20
|
+
options?: SweetErrorOptions;
|
21
|
+
/** The instance name e.g `SomeError` */
|
22
|
+
name?: string;
|
23
|
+
/** The instance code e.g `SomeError.SomethingWrong` */
|
24
|
+
code?: string;
|
25
|
+
/** The number or string to exit the `node.js` process with */
|
26
|
+
exitCode?: number | string;
|
27
|
+
/** The instance captured stack */
|
28
|
+
stack?: any[];
|
29
|
+
/** SweetError utils */
|
30
|
+
func: {
|
31
|
+
/**
|
32
|
+
* Colorize string sentences
|
33
|
+
*
|
34
|
+
* @param {string} sentence string to colorize
|
35
|
+
* @returns {string}
|
36
|
+
*/
|
37
|
+
colorize: (sentence: string) => string;
|
38
|
+
/**
|
39
|
+
* json5 stringifier
|
40
|
+
*
|
41
|
+
* @param {object} obj
|
42
|
+
* @returns {string}
|
43
|
+
*/
|
44
|
+
format: (object: any) => string;
|
45
|
+
/**
|
46
|
+
* Add string indentation
|
47
|
+
*
|
48
|
+
* @param {string} string
|
49
|
+
* @param {number} padding indentation lavel
|
50
|
+
* @returns {string}
|
51
|
+
*/
|
52
|
+
indent: (string: string, padding: number) => string;
|
53
|
+
/**
|
54
|
+
* Controls how lines should wrap
|
55
|
+
*
|
56
|
+
* @param {string} text
|
57
|
+
* @param {number} width
|
58
|
+
* @returns {string}
|
59
|
+
*/
|
60
|
+
wrap: (text: string, width: number) => string;
|
61
|
+
};
|
62
|
+
constructor(messages: any[], options?: SweetErrorOptions);
|
63
|
+
}
|
64
|
+
export {};
|
@@ -0,0 +1,18 @@
|
|
1
|
+
/**
|
2
|
+
* The function that controls how the Error appears and how its parts are arranged
|
3
|
+
*
|
4
|
+
* @memberof SweetError
|
5
|
+
* @example ```js
|
6
|
+
* function logger() {
|
7
|
+
* // you have access to all the properties of the SweetError to customize the way it appears
|
8
|
+
* const { messages, name, code, stack, func } = this;
|
9
|
+
* // also some utils may be helpful
|
10
|
+
* const { format, indent, wrap } = func;
|
11
|
+
* }
|
12
|
+
*
|
13
|
+
* const opts = { logger };
|
14
|
+
*
|
15
|
+
* new SweetError([], opts);
|
16
|
+
* ```
|
17
|
+
*/
|
18
|
+
export declare function logger(): void;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
/**
|
2
|
+
* Controls how lines should wrap
|
3
|
+
*
|
4
|
+
* @param {string} text
|
5
|
+
* @param {number} width
|
6
|
+
* @returns {string}
|
7
|
+
*/
|
8
|
+
export declare function wrap(
|
9
|
+
/** The string to wrap */
|
10
|
+
text: string,
|
11
|
+
/** Line width to break the words at */
|
12
|
+
width?: number): string;
|
package/package.json
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
{
|
2
|
+
"name": "sweet-error",
|
3
|
+
"version": "1.0.1",
|
4
|
+
"description": "Makes nodeJs Error cleaner, more readable and better formatted",
|
5
|
+
"author": "𝖊𝖈𝖍𝖔 <https://github.com/echo-64>",
|
6
|
+
"license": "MIT",
|
7
|
+
"main": "./dist/SweetError.js",
|
8
|
+
"module": "./dist/SweetError.mjs",
|
9
|
+
"types": "./dist/types/SweetError.d.ts",
|
10
|
+
"exports": {
|
11
|
+
".": {
|
12
|
+
"require": "./dist/SweetError.js",
|
13
|
+
"import": "./dist/SweetError.mjs",
|
14
|
+
"types": "./dist/types/SweetError.d.ts"
|
15
|
+
}
|
16
|
+
},
|
17
|
+
"files": [
|
18
|
+
"./dist",
|
19
|
+
"./LICENSE",
|
20
|
+
"./README.md",
|
21
|
+
"./package.json",
|
22
|
+
"./sweet-error.png"
|
23
|
+
],
|
24
|
+
"repository": {
|
25
|
+
"type": "git",
|
26
|
+
"url": "git://github.com/echo-64/sweet-error.git"
|
27
|
+
},
|
28
|
+
"scripts": {
|
29
|
+
"clean": "rm -rf ./dist/*",
|
30
|
+
"watch": "rollup -c -w",
|
31
|
+
"build": "npm run clean && rollup -c"
|
32
|
+
},
|
33
|
+
"devDependencies": {
|
34
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
35
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
36
|
+
"@types/node": "^22.15.23",
|
37
|
+
"tslib": "^2.8.1"
|
38
|
+
},
|
39
|
+
"dependencies": {
|
40
|
+
"chalk": "^4.1.2",
|
41
|
+
"json5": "^2.2.3",
|
42
|
+
"stack-utils": "^2.0.6"
|
43
|
+
}
|
44
|
+
}
|
package/sweet-error.png
ADDED
Binary file
|