sweet-error 1.0.1 → 2.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/CHANGELOG.md ADDED
@@ -0,0 +1,42 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## [2.0.0] - 2025-12-24
6
+
7
+ ### Added
8
+
9
+ - New configuration option: `autoExit` to control process termination.
10
+ - New configuration option `locationStyle` for additional output formats.
11
+ - Exported shared types from `types.ts`
12
+
13
+ ### Changed
14
+
15
+ - Improved JSDoc documentation.
16
+ - Refined console rendering to avoid unnecessary blank lines of missing optional properties.
17
+
18
+ ### Removed
19
+
20
+ - Removed the `options` instance property.
21
+ - Removed the `logger` instance property.
22
+ - Renamed `SweetErrorOptions` to `Config`.
23
+ - Removed named export `SweetError`, the package now uses a default export.
24
+
25
+ ### Migration Notes
26
+
27
+ - The `error.options` property has been removed.
28
+ Replace any usage of `error.options` with the corresponding instance properties
29
+ (e.g. `error.autoExit`, `error.locationStyle`, `error.colorize`).
30
+
31
+ - The `logger` function is no longer exposed as an instance property.
32
+ Custom logging logic must be provided through the configuration object at initialization.
33
+
34
+ - Update TypeScript imports from `SweetErrorOptions` to `ErrorConfig`
35
+
36
+ - Update imports to use the default export instead of a named export.
37
+
38
+ ## [1.0.1] - 2025-06-7
39
+
40
+ ### Added
41
+
42
+ - Initial release of `sweet-error`
@@ -0,0 +1,7 @@
1
+ Contributions are welcome in all forms.
2
+
3
+ You can help by:
4
+ - Opening new issues for bugs, questions, or feature requests
5
+ - Suggesting improvements to documentation or API design
6
+
7
+ If you’re unsure whether something is worth reporting, feel free to open an issue and start a discussion.
package/README.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  Makes nodeJs Error cleaner, more readable and better formatted
4
4
 
5
+ - [SweetError](#sweeterror)
6
+ - [Installation](#installation)
7
+ - [Example usage](#example-usage)
8
+ - [Api reference](#api-reference)
9
+ - [Configuration Properties](#configuration-properties)
10
+ - [Instance properties](#instance-properties)
11
+ - [License](#license)
12
+ - [Changelog](#changelog)
13
+
5
14
  ## Installation
6
15
 
7
16
  #### NodeJs
@@ -10,13 +19,13 @@ Makes nodeJs Error cleaner, more readable and better formatted
10
19
  npm install sweet-error
11
20
  ```
12
21
 
13
- ## Example
22
+ ## Example usage
14
23
 
15
24
  ```js
16
25
  // cjs
17
- const { SweetError } = require('sweet-error');
26
+ const SweetError = require('sweet-error');
18
27
  // mjs
19
- import { SweetError } from 'sweet-error';
28
+ import SweetError from 'sweet-error';
20
29
 
21
30
  new SweetError(
22
31
  [
@@ -35,81 +44,235 @@ new SweetError(
35
44
  <br />
36
45
 
37
46
  <div align='center'>
38
- <img src="./sweet-error.png" style='border-radius: 10px'></img>
47
+ <img src="./screenshots/sweet-error.png" style='border-radius: 10px'></img>
39
48
  </div>
40
49
 
41
- ## Api
50
+ ## Api reference
51
+
52
+ ### `new SweetError(messages, [config])`
53
+
54
+ Creates a new `SweetError` instance.
55
+
56
+ **Parameters**:
57
+
58
+ - `messages` — An array of messages, where each message can be of any data type, and each index in the array represents a line.
59
+
60
+ - **Required**
61
+ - **Type:** `any[]`
62
+
63
+ - `config` — Optional configuration object, see [configuration properties](#configuration-properties)
64
+
65
+ - **Optional**
66
+ - **Type:** `Object`
67
+
68
+ ## Configuration Properties
69
+
70
+ ### `name`
71
+
72
+ Sets the name of the current error instance.
73
+
74
+ This is typically used to identify the type of error when logging or displaying it in the console.
75
+
76
+ - **Optional**
77
+ - **Type:** `string`
78
+ - **Default:** `undefined`
79
+
80
+ ### `code`
81
+
82
+ Sets the current instance error code.
83
+
84
+ This can be used to programmatically identify or handle specific error types.
85
+
86
+ - **Optional**
87
+ - **Type:** `string`
88
+ - **Default:** `undefined`
89
+
90
+ **Example** :
42
91
 
43
92
  ```js
44
- const error = new SweetError(messages, [options]);
93
+ new SweetError(['ACCESERR Error!'], {
94
+ code: 'ACCESERR',
95
+ });
45
96
  ```
46
97
 
47
- ### messages
98
+ ### `exitCode`
48
99
 
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.
100
+ Sets the code or signal name used to terminate the Node.js process when the instance triggers an exit.
52
101
 
53
- ### options
102
+ - **Optional**
103
+ - **Type:** `number | string`
104
+ - **Default:** `undefined`
54
105
 
55
- - **name**
56
- Type: `string`
57
- The string represents the instance name e.g: `SomeError`
106
+ ### `autoExit`
58
107
 
59
- - **code**
60
- Type: `string`
61
- The string represents the instance code e.g: `SomeError.SomethingWrong`
108
+ Controls whether the Node.js process should automatically exit after the error is logged.
62
109
 
63
- - **exitCode**
64
- Type: `string | number`
65
- The code to exit the `node.js` process with
110
+ Set this to `true` to terminate the process immediately after logging, or `false` to continue running and handle termination manually.
66
111
 
67
- - **colorize**
68
- Type: `boolean`
69
- Default: `true`
70
- Enables or disables text colorization
112
+ - **Optional**
113
+ - **Type:** `boolean`
114
+ - **Default:** `true`
71
115
 
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
116
+ **Example** :
75
117
 
76
- ### error.messages
118
+ ```js
119
+ new SweetError({
120
+ autoExit: false,
121
+ });
122
+
123
+ console.log('The process will NOT exit automatically');
124
+ ```
125
+
126
+ ### `colorize`
127
+
128
+ Enables or disables text colorization in the console output.
129
+
130
+ When set to `true`, the error messages will include colors for better readability.
131
+ When `false`, all output will be plain text.
132
+
133
+ - **Optional**
134
+ - **Type:** `boolean`
135
+ - **Default:** `true`
136
+
137
+ ### `locationStyle`
138
+
139
+ Determines how the error location (file, line, column) is formatted when displayed in the console.
77
140
 
78
- Type: `any[]`
79
- Returns the specified messages array that is used to initialize the instance
141
+ This allows customization of the visual style of the location information for improved readability.
80
142
 
81
- ### error.options
143
+ - **Optional**
144
+ - **Type:** `LocationStyle`
145
+ - **Default:** `'label'`
82
146
 
83
- Type: `SweetErrorOptions | undefined`
84
- Returns the options object that the instance initialized with
147
+ **Example** :
85
148
 
86
- ### error.name
149
+ - `locationStyle: label` — For readable, human-friendly format.
87
150
 
88
- Type: `string | undefined`
89
- Returns the instance name
151
+ ![locationStyle: label](./screenshots/location-style-label.png)
90
152
 
91
- ### error.code
153
+ - `locationStyle: coords` — Clickable links for IDEs / Standard IDE format.
92
154
 
93
- Type: `string | undefined`
94
- Returns the instance code of the error
155
+ ![locationStyle: coords](./screenshots/location-style-coords.png)
95
156
 
96
- ### error.exitCode
157
+ - `locationStyle: full` — For most formal and complete format.
158
+
159
+ ![locationStyle: full](./screenshots/location-style-full.png)
160
+
161
+ ### `logger`
162
+
163
+ A custom function to control how the `SweetError` output is rendered.
164
+ This function is provided via the configuration object when creating a new instance.
165
+
166
+ You can override this function to implement custom logging behavior, such as formatting output differently, or integrating with other logging systems.
167
+
168
+ - **Optional**
169
+ - **Type:** `(this: SweetError) => void`
170
+
171
+ **Example:**
172
+
173
+ ```js
174
+ new SweetError(['Custom output!'], {
175
+ logger() {
176
+ console.log(this.func.colorize('Custom error message'));
177
+ },
178
+ });
179
+ ```
97
180
 
98
- Type: `string | number | undefined`
99
- Returns the instance exit code that was used to exit the runtime.
181
+ ## Instance properties
100
182
 
101
- ### error.stack
183
+ ### `.messages`
102
184
 
103
- Type: `any[]`
104
- Returns an array of objects representing the captured instance stack.
185
+ An array of messages used to initialize the error instance.
105
186
 
106
- ### error.func
187
+ - **Type:** `any[]`
107
188
 
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.
189
+ ### `.name`
110
190
 
111
- <h4 align='center'>Please Hit the button if you found this useful</h4>
191
+ The name of the error instance.
192
+
193
+ - **Type:** `string`
194
+ - **Default:** `undefined`
195
+
196
+ ### `.code`
197
+
198
+ The error code associated with this instance.
199
+
200
+ - **Type:** `string`
201
+ - **Default:** `undefined`
202
+
203
+ ### `.exitCode`
204
+
205
+ The code or signal name used to terminate the Node.js process.
206
+
207
+ - **Type:** `number | string`
208
+ - **Default:** `undefined`
209
+
210
+ ### `.autoExit`
211
+
212
+ Indicates whether the `SweetError` instance is configured to automatically exit the Node.js process after logging.
213
+
214
+ - **Type:** `boolean`
215
+ - **Default:** `true`
216
+
217
+ ### `.locationStyle`
218
+
219
+ Indicates how the error location (file, line, column) is formatted for this `SweetError` instance when rendered in the console.
220
+
221
+ - **Type:** `LocationStyle`
222
+ - **Default:** `'label'`
223
+
224
+ ### `.colorize`
225
+
226
+ Indicates whether the `SweetError` instance will render messages with color in the console.
227
+
228
+ - **Type:** `boolean`
229
+ - **Default:** `true`
230
+
231
+ ### `.stack`
232
+
233
+ The stack trace captured by the `SweetError` instance at the time of creation.
234
+
235
+ - **Type:** `StackData[]`
236
+ - **Readonly**
237
+
238
+ ### `.func`
239
+
240
+ A read-only collection of string formatting and transformation utilities available on the `SweetError` instance.
241
+
242
+ These utilities are primarily intended for use **inside a custom `logger` function** to help customize the format and appearance of the error output.
243
+
244
+ - **Readonly**
245
+
246
+ #### Utilities
247
+
248
+ - `colorize(sentence: string): string`
249
+ Applies random terminal colors to each word in a sentence using ANSI codes.
250
+
251
+ - `format(object: any): string`
252
+ Serializes an object into a JSON5-formatted string for pretty printing.
253
+
254
+ - `indent(string: string, padding: number): string`
255
+ Prepends a specific number of spaces to every line in a multiline string.
256
+
257
+ - `wrap(text: string, width: number): string`
258
+ Breaks a long string into multiple lines based on the specified character width.
259
+
260
+ **Example** :
261
+
262
+ ```js
263
+ new SweetError(['Error Occurred!'], {
264
+ logger() {
265
+ const { wrap } = this.func;
266
+
267
+ console.log(wrap('This is a long message that will be wrapped', 40));
268
+ },
269
+ });
270
+ ```
112
271
 
113
272
  ## License
114
273
 
115
- [MIT License ©](./LICENSE)
274
+ This project is licensed under the MIT License — see the [LICENSE](./LICENSE) file for details.
275
+
276
+ ## Changelog
277
+
278
+ See the [CHANGELOG](./CHANGELOG.md) for a detailed history of all changes, versions, and releases.
@@ -0,0 +1,154 @@
1
+ import { StackData } from '../node_modules/@types/stack-utils';
2
+
3
+ interface ErrorConfig {
4
+ /**
5
+ * The instance name e.g `SomeError`
6
+ *
7
+ * @default undefined
8
+ * */
9
+ name?: string;
10
+ /**
11
+ * The instance error code e.g `SomeError.SomethingWrong`
12
+ *
13
+ * @default undefined
14
+ * */
15
+ code?: string;
16
+ /**
17
+ * The exit code or signal name used when the Node process terminates.
18
+ *
19
+ * @default 0
20
+ * */
21
+ exitCode?: string | number;
22
+ /**
23
+ * Whether to automatically terminate the process after logging.
24
+ *
25
+ * @default true
26
+ */
27
+ autoExit?: boolean;
28
+ /**
29
+ * Enables or disables text colorization
30
+ *
31
+ * @default true
32
+ * */
33
+ colorize?: boolean;
34
+ /**
35
+ * Determines how line and column information is displayed:
36
+ * - `label`: For readable, human-friendly format.
37
+ * > `line: 3 column: 1 file: index.ts`
38
+ * - `coords`: Clickable links for IDEs / Standard IDE format.
39
+ * > `file: index.ts:3:1`
40
+ * - `full`: For most formal and complete format.
41
+ * > `line: 3 column: 1 file: index.ts:3:1`
42
+ *
43
+ * @default 'label'
44
+ */
45
+ locationStyle?: LocationStyle;
46
+ /** Controls how should `SweetError` output looks and organized */
47
+ logger?: (this: SweetError) => void;
48
+ }
49
+ /**
50
+ * Determines how line and column information is displayed:
51
+ * - `label`: For readable, human-friendly format.
52
+ * > `line: 3 column: 1 file: index.ts`
53
+ * - `coords`: Clickable links for IDEs / Standard IDE format.
54
+ * > `file: index.ts:3:1`
55
+ * - `full`: For most formal and complete format.
56
+ * > `line: 3 column: 1 file: index.ts:3:1`
57
+ */
58
+ type LocationStyle = 'label' | 'coords' | 'full';
59
+
60
+ declare class SweetError {
61
+ /** An array of messages, each index represents a line */
62
+ messages: any[];
63
+ /**
64
+ * The instance name e.g., `SomeError`
65
+ *
66
+ * @default undefined
67
+ * */
68
+ name?: string;
69
+ /**
70
+ * The instance code e.g., `SomeError.SomethingWrong`
71
+ *
72
+ * @default undefined
73
+ * */
74
+ code?: string;
75
+ /**
76
+ * The exit code or signal name used when the Node process terminates.
77
+ *
78
+ * @default undefined
79
+ * */
80
+ exitCode?: number | string;
81
+ /**
82
+ * Whether to automatically terminate the process after logging.
83
+ *
84
+ * @default true
85
+ */
86
+ autoExit: boolean;
87
+ /**
88
+ * Determines how line and column information is displayed:
89
+ * - `label`: For readable, human-friendly format.
90
+ * > `line: 3 column: 1 file: index.ts`
91
+ * - `coords`: Clickable links for IDEs / Standard IDE format.
92
+ * > `file: index.ts:3:1`
93
+ * - `full`: For most formal and complete format.
94
+ * > `line: 3 column: 1 file: index.ts:3:1`
95
+ *
96
+ * @default 'label'
97
+ */
98
+ locationStyle: LocationStyle;
99
+ /**
100
+ * Enables or disables text colorization
101
+ *
102
+ * @default true
103
+ * */
104
+ colorize: boolean;
105
+ /**
106
+ * The instance captured stack.
107
+ *
108
+ * @readonly
109
+ * */
110
+ readonly stack: StackData[];
111
+ /**
112
+ * A collection of string formatting and transformation utilities.
113
+ *
114
+ * @readonly
115
+ * */
116
+ readonly func: {
117
+ /**
118
+ * Applies random terminal colors to each word in a sentence.
119
+ *
120
+ * @param {string} sentence - The string to colorize.
121
+ * @returns {string} The colorized string with ANSI codes.
122
+ */
123
+ colorize: (sentence: string) => string;
124
+ /**
125
+ * Serializes an object into a JSON5 formatted string.
126
+ *
127
+ * @param {any} object - The object to stringify.
128
+ * @returns {string} The formatted JSON5 string.
129
+ */
130
+ format: (object: any) => string;
131
+ /**
132
+ * Prepends a specific number of spaces to every line in a string.
133
+ *
134
+ * @param {string} string - The multiline string to indent.
135
+ * @param {number} padding - The number of spaces to add.
136
+ * @returns {string}
137
+ */
138
+ indent: (string: string, padding: number) => string;
139
+ /**
140
+ * Breaks a long string into multiple lines based on character width.
141
+ *
142
+ * @param {string} text - The text to wrap.
143
+ * @param {number} width - The maximum line length.
144
+ * @returns {string}
145
+ */
146
+ wrap: (text: string, width: number) => string;
147
+ };
148
+ /**
149
+ * Creates a new `SweetError` instance.
150
+ */
151
+ constructor(messages: any[], config?: ErrorConfig);
152
+ }
153
+
154
+ export { type ErrorConfig, type LocationStyle, SweetError as default };