@hkdigital/lib-core 0.4.16 → 0.4.17
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/dist/generic/errors/generic.d.ts +6 -10
- package/dist/generic/errors/generic.js +14 -4
- package/dist/generic/errors/typedef.d.ts +5 -0
- package/dist/generic/errors/typedef.js +3 -0
- package/dist/generic/typedef.d.ts +1 -0
- package/dist/generic/typedef.js +1 -0
- package/dist/logging/errors.js +8 -1
- package/dist/logging/internal/adapters/console.js +0 -2
- package/dist/logging/internal/logger/Logger.d.ts +7 -4
- package/dist/logging/internal/logger/Logger.js +139 -70
- package/dist/util/exceptions/index.d.ts +2 -2
- package/dist/util/exceptions/index.js +8 -5
- package/dist/util/object/index.d.ts +1 -1
- package/dist/util/object/index.js +1 -1
- package/package.json +1 -1
- package/dist/logging/internal/logger/util.d.ts +0 -13
- package/dist/logging/internal/logger/util.js +0 -47
|
@@ -6,15 +6,11 @@ export class InternalEventOrLogError extends Error {
|
|
|
6
6
|
}
|
|
7
7
|
export class DetailedError extends Error {
|
|
8
8
|
/**
|
|
9
|
-
* @param {string} message
|
|
10
|
-
* @param {
|
|
11
|
-
* @param {Error|
|
|
9
|
+
* @param {string} [message]
|
|
10
|
+
* @param {import('./typedef.js').ErrorDetails} [details] - Additional details
|
|
11
|
+
* @param {Error|null} [cause] - Original error
|
|
12
12
|
*/
|
|
13
|
-
constructor(message
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
details: string | {
|
|
17
|
-
[key: string]: any;
|
|
18
|
-
};
|
|
19
|
-
cause: string | Error;
|
|
13
|
+
constructor(message?: string, details?: import("./typedef.js").ErrorDetails, cause?: Error | null);
|
|
14
|
+
details: import("./typedef.js").ErrorDetails;
|
|
15
|
+
cause: unknown;
|
|
20
16
|
}
|
|
@@ -7,14 +7,24 @@ export class InternalEventOrLogError extends Error {}
|
|
|
7
7
|
export class DetailedError extends Error
|
|
8
8
|
{
|
|
9
9
|
/**
|
|
10
|
-
* @param {string} message
|
|
11
|
-
* @param {
|
|
12
|
-
* @param {Error|
|
|
10
|
+
* @param {string} [message]
|
|
11
|
+
* @param {import('./typedef.js').ErrorDetails} [details] - Additional details
|
|
12
|
+
* @param {Error|null} [cause] - Original error
|
|
13
13
|
*/
|
|
14
14
|
constructor(message, details, cause ) {
|
|
15
15
|
super(message);
|
|
16
16
|
this.name = 'DetailedError';
|
|
17
17
|
this.details = details ?? null;
|
|
18
|
-
|
|
18
|
+
|
|
19
|
+
if( cause )
|
|
20
|
+
{
|
|
21
|
+
if( cause instanceof Error )
|
|
22
|
+
{
|
|
23
|
+
this.cause = cause;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
throw new Error('Parameter [cause] should be an Error');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
19
29
|
}
|
|
20
30
|
}
|
package/dist/generic/typedef.js
CHANGED
package/dist/logging/errors.js
CHANGED
|
@@ -6,6 +6,13 @@ export class LoggerError extends Error
|
|
|
6
6
|
constructor( originalError ) {
|
|
7
7
|
super('LoggerError');
|
|
8
8
|
this.name = 'LoggerError';
|
|
9
|
-
|
|
9
|
+
|
|
10
|
+
if( originalError instanceof Error )
|
|
11
|
+
{
|
|
12
|
+
this.cause = originalError;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
throw new Error('Parameter [originalError] should be an Error');
|
|
16
|
+
}
|
|
10
17
|
}
|
|
11
18
|
}
|
|
@@ -360,8 +360,6 @@ export class ConsoleAdapter {
|
|
|
360
360
|
|
|
361
361
|
let cleaned = trimmed;
|
|
362
362
|
|
|
363
|
-
console.log(123, cleaned);
|
|
364
|
-
|
|
365
363
|
// Convert Chrome format to Firefox format for consistency
|
|
366
364
|
if (isChromeFormat) {
|
|
367
365
|
// "at functionName (url:line:col)" -> "functionName@url:line:col"
|
|
@@ -50,15 +50,17 @@ export default class Logger extends EventEmitter {
|
|
|
50
50
|
* Log an error message
|
|
51
51
|
*
|
|
52
52
|
* @param {Error|ErrorEvent|PromiseRejectionEvent|string}
|
|
53
|
-
*
|
|
54
|
-
*
|
|
53
|
+
* errorOrMessage
|
|
54
|
+
*
|
|
55
|
+
* @param {Error|ErrorDetails} [errorOrDetails]
|
|
55
56
|
* Error object (when first param is string) or details object
|
|
56
|
-
*
|
|
57
|
+
*
|
|
58
|
+
* @param {ErrorDetails} [details]
|
|
57
59
|
* Additional context details (when using string + error pattern)
|
|
58
60
|
*
|
|
59
61
|
* @returns {boolean} True if the log was emitted
|
|
60
62
|
*/
|
|
61
|
-
error(
|
|
63
|
+
error(errorOrMessage: Error | ErrorEvent | PromiseRejectionEvent | string, errorOrDetails?: Error | ErrorDetails, details?: ErrorDetails, ...args: any[]): boolean;
|
|
62
64
|
/**
|
|
63
65
|
* Create a child logger with additional context
|
|
64
66
|
*
|
|
@@ -81,4 +83,5 @@ export default class Logger extends EventEmitter {
|
|
|
81
83
|
logFromEvent(eventName: string, eventData: import("../../typedef.js").LogEventData): boolean;
|
|
82
84
|
#private;
|
|
83
85
|
}
|
|
86
|
+
export type ErrorDetails = import("../../../generic/typedef.js").ErrorDetails;
|
|
84
87
|
import { EventEmitter } from '../../../generic/events.js';
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
* logger.setLevel(DEBUG); // Now debug messages will also be logged
|
|
33
33
|
*/
|
|
34
34
|
|
|
35
|
+
/** @typedef {import('../../../generic/typedef.js').ErrorDetails} ErrorDetails */
|
|
36
|
+
|
|
35
37
|
import { EventEmitter } from '../../../generic/events.js';
|
|
36
38
|
|
|
37
39
|
import {
|
|
@@ -44,14 +46,15 @@ import {
|
|
|
44
46
|
} from '../../constants.js';
|
|
45
47
|
|
|
46
48
|
import { DetailedError } from '../../../generic/errors.js';
|
|
47
|
-
import { LoggerError } from '../../errors.js';
|
|
49
|
+
// import { LoggerError } from '../../errors.js';
|
|
48
50
|
|
|
49
|
-
import { toArray } from '../../../util/array
|
|
51
|
+
import { toArray } from '../../../util/array.js';
|
|
52
|
+
import { exportNotNullish } from '../../../util/object.js';
|
|
50
53
|
|
|
51
|
-
import {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
} from './util.js';
|
|
54
|
+
// import {
|
|
55
|
+
// castErrorEventToDetailedError,
|
|
56
|
+
// castPromiseRejectionToDetailedError
|
|
57
|
+
// } from './util.js';
|
|
55
58
|
|
|
56
59
|
import * as is from '../../../util/is.js';
|
|
57
60
|
|
|
@@ -134,91 +137,77 @@ export default class Logger extends EventEmitter {
|
|
|
134
137
|
* Log an error message
|
|
135
138
|
*
|
|
136
139
|
* @param {Error|ErrorEvent|PromiseRejectionEvent|string}
|
|
137
|
-
*
|
|
138
|
-
*
|
|
140
|
+
* errorOrMessage
|
|
141
|
+
*
|
|
142
|
+
* @param {Error|ErrorDetails} [errorOrDetails]
|
|
139
143
|
* Error object (when first param is string) or details object
|
|
140
|
-
*
|
|
144
|
+
*
|
|
145
|
+
* @param {ErrorDetails} [details]
|
|
141
146
|
* Additional context details (when using string + error pattern)
|
|
142
147
|
*
|
|
143
148
|
* @returns {boolean} True if the log was emitted
|
|
144
149
|
*/
|
|
145
|
-
error(
|
|
146
|
-
|
|
147
|
-
let message;
|
|
148
|
-
let errorDetails;
|
|
149
|
-
let cause;
|
|
150
|
+
error(errorOrMessage, errorOrDetails, details) {
|
|
151
|
+
let errorObject = null;
|
|
150
152
|
|
|
151
|
-
|
|
152
|
-
if (is.ErrorEvent(originalErrorOrMessage)) {
|
|
153
|
-
const errorEvent = /** @type {ErrorEvent} */ (originalErrorOrMessage);
|
|
153
|
+
let message;
|
|
154
154
|
|
|
155
|
-
|
|
156
|
-
|
|
155
|
+
if( typeof errorOrMessage === 'string' )
|
|
156
|
+
{
|
|
157
|
+
message = errorOrMessage;
|
|
158
|
+
}
|
|
157
159
|
|
|
158
|
-
|
|
160
|
+
if (message) {
|
|
159
161
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
lineno: errorEvent.lineno,
|
|
163
|
-
colno: errorEvent.colno,
|
|
164
|
-
type: 'ErrorEvent'
|
|
165
|
-
};
|
|
162
|
+
// First param is a string (message)
|
|
163
|
+
// => Second param might be the error (string + error pattern)
|
|
166
164
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
/** @type {PromiseRejectionEvent} */ (originalErrorOrMessage);
|
|
165
|
+
if (this.#isErrorLike(errorOrDetails)) {
|
|
166
|
+
let errorLike = errorOrDetails;
|
|
167
|
+
errorObject = this.#toError(errorLike);
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
173
170
|
|
|
174
|
-
|
|
171
|
+
// First param is not an (not empty) string
|
|
172
|
+
// => First param should be the error
|
|
175
173
|
|
|
176
|
-
if (
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
} else {
|
|
180
|
-
message = String(reason);
|
|
181
|
-
cause = null;
|
|
174
|
+
if (this.#isErrorLike(errorOrMessage)) {
|
|
175
|
+
let errorLike = errorOrMessage;
|
|
176
|
+
errorObject = this.#toError(errorLike);
|
|
182
177
|
}
|
|
183
178
|
|
|
184
|
-
//
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
reasonType: typeof reason
|
|
188
|
-
};
|
|
179
|
+
// Second parameter could be the details
|
|
180
|
+
// Third parameter will be ignored (overwritten)
|
|
181
|
+
details = errorOrDetails;
|
|
189
182
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
183
|
+
|
|
184
|
+
if( errorObject && details )
|
|
185
|
+
{
|
|
186
|
+
// Additional Details supplied
|
|
187
|
+
// => Prepend additional DetailedError to chain
|
|
188
|
+
errorObject = new DetailedError(message, details, errorObject );
|
|
195
189
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
message = originalErrorOrMessage;
|
|
202
|
-
errorDetails = details || null;
|
|
203
|
-
cause = originalErrorOrDetails;
|
|
190
|
+
|
|
191
|
+
if( message && errorObject )
|
|
192
|
+
{
|
|
193
|
+
// Log with message
|
|
194
|
+
return this.#log(ERROR, message, errorObject);
|
|
204
195
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
message
|
|
208
|
-
errorDetails = originalErrorOrDetails || null;
|
|
209
|
-
cause = null;
|
|
196
|
+
else if (errorObject) {
|
|
197
|
+
// Log without message
|
|
198
|
+
return this.#log(ERROR, errorObject.message, errorObject);
|
|
210
199
|
}
|
|
211
|
-
// Invalid parameters
|
|
212
200
|
else {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
cause = null;
|
|
216
|
-
}
|
|
201
|
+
// Missing error like object
|
|
202
|
+
// => invalid parameters supplied to logger.error
|
|
217
203
|
|
|
218
|
-
|
|
219
|
-
|
|
204
|
+
const detailedError = new DetailedError(
|
|
205
|
+
'Missing error like object in Logger.error parameters',
|
|
206
|
+
toArray(arguments)
|
|
207
|
+
);
|
|
220
208
|
|
|
221
|
-
|
|
209
|
+
return this.#log(ERROR, detailedError.message, detailedError);
|
|
210
|
+
}
|
|
222
211
|
}
|
|
223
212
|
|
|
224
213
|
/**
|
|
@@ -307,4 +296,84 @@ export default class Logger extends EventEmitter {
|
|
|
307
296
|
|
|
308
297
|
return true;
|
|
309
298
|
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Returns true is the supplied parameter is loggable as error
|
|
302
|
+
*
|
|
303
|
+
* @param {any} thing
|
|
304
|
+
*
|
|
305
|
+
* @returns {boolean}
|
|
306
|
+
*/
|
|
307
|
+
#isErrorLike(thing) {
|
|
308
|
+
return (
|
|
309
|
+
thing instanceof Error ||
|
|
310
|
+
is.ErrorEvent(thing) ||
|
|
311
|
+
is.PromiseRejectionEvent(thing)
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Convert an Error like object (Error, ErrorEvent, PromiseRejectionEvent)
|
|
317
|
+
* to a DetailedError
|
|
318
|
+
*
|
|
319
|
+
* @param {any} errorLike
|
|
320
|
+
*
|
|
321
|
+
* @returns {Error}
|
|
322
|
+
*/
|
|
323
|
+
#toError( errorLike ) {
|
|
324
|
+
if (is.ErrorEvent(errorLike)) {
|
|
325
|
+
// errorLike is an ErrorEvent
|
|
326
|
+
// => convert to DetailedError
|
|
327
|
+
const errorEvent = /** @type {ErrorEvent} */ (errorLike);
|
|
328
|
+
|
|
329
|
+
let errorEventDetails = exportNotNullish( errorEvent, [
|
|
330
|
+
'message',
|
|
331
|
+
'filename',
|
|
332
|
+
'lineno',
|
|
333
|
+
'colno'
|
|
334
|
+
]);
|
|
335
|
+
|
|
336
|
+
errorEventDetails.type = 'ErrorEvent';
|
|
337
|
+
|
|
338
|
+
let cause = errorEvent.error;
|
|
339
|
+
|
|
340
|
+
return new DetailedError(
|
|
341
|
+
errorEvent.message,
|
|
342
|
+
errorEventDetails,
|
|
343
|
+
cause
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
} else if (is.PromiseRejectionEvent(errorLike)) {
|
|
347
|
+
// errorLike is a PromiseRejectionEvent
|
|
348
|
+
// => convert to DetailedError
|
|
349
|
+
const rejectionEvent = /** @type {PromiseRejectionEvent} */ (errorLike);
|
|
350
|
+
|
|
351
|
+
const reason = rejectionEvent.reason;
|
|
352
|
+
|
|
353
|
+
if (reason instanceof Error) {
|
|
354
|
+
return reason;
|
|
355
|
+
}
|
|
356
|
+
else if ( is.object(reason) && reason?.message ) {
|
|
357
|
+
// reason is an object with message property
|
|
358
|
+
return new DetailedError(reason?.message, reason);
|
|
359
|
+
}
|
|
360
|
+
else if ( typeof reason === "string" ) {
|
|
361
|
+
// reason is a string
|
|
362
|
+
return new DetailedError(reason);
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
// reason is not an Error or string
|
|
366
|
+
return new DetailedError('Promise rejected', reason);
|
|
367
|
+
}
|
|
368
|
+
} else if (errorLike instanceof Error) {
|
|
369
|
+
// errorLike is a plain Error
|
|
370
|
+
// => return it
|
|
371
|
+
return errorLike;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// errorLike cannot be converted to an Error
|
|
375
|
+
// => return logging error
|
|
376
|
+
|
|
377
|
+
return new DetailedError('Cannot convert to Error', errorLike);
|
|
378
|
+
}
|
|
310
379
|
}
|
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
* - The originating Error is set as `cause` property
|
|
5
5
|
*
|
|
6
6
|
* @param {string} message - New error message
|
|
7
|
-
* @param {Error|string}
|
|
7
|
+
* @param {Error|string} originalErrorOrMessage
|
|
8
8
|
* @param {string|Object.<string, any>|null} [details]
|
|
9
9
|
* New error details (a DetailError will be thrown)
|
|
10
10
|
*
|
|
11
11
|
* @throws {DetailedError}
|
|
12
12
|
* @returns {never} This function never returns
|
|
13
13
|
*/
|
|
14
|
-
export function rethrow(message: string,
|
|
14
|
+
export function rethrow(message: string, originalErrorOrMessage: Error | string, details?: string | {
|
|
15
15
|
[x: string]: any;
|
|
16
16
|
} | null): never;
|
|
@@ -7,20 +7,23 @@ import { DetailedError } from "../../generic/errors.js";
|
|
|
7
7
|
* - The originating Error is set as `cause` property
|
|
8
8
|
*
|
|
9
9
|
* @param {string} message - New error message
|
|
10
|
-
* @param {Error|string}
|
|
10
|
+
* @param {Error|string} originalErrorOrMessage
|
|
11
11
|
* @param {string|Object.<string, any>|null} [details]
|
|
12
12
|
* New error details (a DetailError will be thrown)
|
|
13
13
|
*
|
|
14
14
|
* @throws {DetailedError}
|
|
15
15
|
* @returns {never} This function never returns
|
|
16
16
|
*/
|
|
17
|
-
export function rethrow(message,
|
|
17
|
+
export function rethrow(message, originalErrorOrMessage, details ) {
|
|
18
18
|
let error;
|
|
19
19
|
|
|
20
|
-
if (
|
|
20
|
+
if ( typeof originalErrorOrMessage === 'string' ) {
|
|
21
21
|
// Convert non-Error values to Error objects
|
|
22
|
-
error = new Error(
|
|
22
|
+
error = new Error( originalErrorOrMessage );
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
error = originalErrorOrMessage;
|
|
23
26
|
}
|
|
24
27
|
|
|
25
|
-
throw new DetailedError(message, details, error
|
|
28
|
+
throw new DetailedError(message, details, error );
|
|
26
29
|
}
|
|
@@ -29,7 +29,7 @@ export function size(obj: object): number;
|
|
|
29
29
|
*
|
|
30
30
|
* @returns {object} new object without the null properties
|
|
31
31
|
*/
|
|
32
|
-
export function
|
|
32
|
+
export function exportNotNullish(obj: object, onlyKeys?: string[]): object;
|
|
33
33
|
/**
|
|
34
34
|
* Create a shallow copy of the object's public properties. Properties that
|
|
35
35
|
* start with an underscore are considered 'internal' properties and are not
|
|
@@ -90,7 +90,7 @@ export function size(obj) {
|
|
|
90
90
|
*
|
|
91
91
|
* @returns {object} new object without the null properties
|
|
92
92
|
*/
|
|
93
|
-
export function
|
|
93
|
+
export function exportNotNullish(obj, onlyKeys) {
|
|
94
94
|
expect.object(obj);
|
|
95
95
|
|
|
96
96
|
const onlyKeysSet = onlyKeys ? new Set(onlyKeys) : null;
|
package/package.json
CHANGED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cast ErrorEvent to DetailedError
|
|
3
|
-
* @param {ErrorEvent} errorEvent - Browser ErrorEvent object
|
|
4
|
-
* @returns {DetailedError}
|
|
5
|
-
*/
|
|
6
|
-
export function castErrorEventToDetailedError(errorEvent: ErrorEvent): DetailedError;
|
|
7
|
-
/**
|
|
8
|
-
* Cast PromiseRejectionEvent to DetailedError
|
|
9
|
-
* @param {PromiseRejectionEvent} rejectionEvent - Browser promise rejection
|
|
10
|
-
* @returns {DetailedError}
|
|
11
|
-
*/
|
|
12
|
-
export function castPromiseRejectionToDetailedError(rejectionEvent: PromiseRejectionEvent): DetailedError;
|
|
13
|
-
import { DetailedError } from '../../../generic/errors/generic.js';
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { DetailedError } from '../../../generic/errors/generic.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Cast ErrorEvent to DetailedError
|
|
5
|
-
* @param {ErrorEvent} errorEvent - Browser ErrorEvent object
|
|
6
|
-
* @returns {DetailedError}
|
|
7
|
-
*/
|
|
8
|
-
export function castErrorEventToDetailedError(errorEvent) {
|
|
9
|
-
const message = errorEvent.error?.message ||
|
|
10
|
-
errorEvent.message ||
|
|
11
|
-
'Unknown error';
|
|
12
|
-
|
|
13
|
-
const details = {
|
|
14
|
-
filename: errorEvent.filename,
|
|
15
|
-
lineno: errorEvent.lineno,
|
|
16
|
-
colno: errorEvent.colno,
|
|
17
|
-
type: 'ErrorEvent'
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
return new DetailedError(message, details, errorEvent.error);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Cast PromiseRejectionEvent to DetailedError
|
|
25
|
-
* @param {PromiseRejectionEvent} rejectionEvent - Browser promise rejection
|
|
26
|
-
* @returns {DetailedError}
|
|
27
|
-
*/
|
|
28
|
-
export function castPromiseRejectionToDetailedError(rejectionEvent) {
|
|
29
|
-
const reason = rejectionEvent.reason;
|
|
30
|
-
let message, cause;
|
|
31
|
-
|
|
32
|
-
if (reason instanceof Error) {
|
|
33
|
-
message = reason.message;
|
|
34
|
-
cause = reason;
|
|
35
|
-
} else {
|
|
36
|
-
message = String(reason);
|
|
37
|
-
cause = null;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const details = {
|
|
41
|
-
type: 'PromiseRejectionEvent',
|
|
42
|
-
reasonType: typeof reason
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
return new DetailedError(message, details, cause);
|
|
46
|
-
}
|
|
47
|
-
|