@hkdigital/lib-core 0.4.16 → 0.4.18

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.
@@ -109,13 +109,13 @@ function generateColorStyles() {
109
109
  for (const colorName of colorNames) {
110
110
  colors[colorName] = {};
111
111
  for (const shade of shades) {
112
- colors[colorName][shade] = `rgb(var(--color-${colorName}-${shade}) / <alpha-value>)`;
112
+ colors[colorName][shade] = `var(--color-${colorName}-${shade})`;
113
113
  }
114
114
 
115
115
  // Add contrast colors
116
116
  for (const shade of shades) {
117
117
  colors[`${colorName}-contrast`] = colors[`${colorName}-contrast`] || {};
118
- colors[`${colorName}-contrast`][shade] = `rgb(var(--color-${colorName}-contrast-${shade}) / <alpha-value>)`;
118
+ colors[`${colorName}-contrast`][shade] = `var(--color-${colorName}-contrast-${shade})`;
119
119
  }
120
120
  }
121
121
 
@@ -107,7 +107,7 @@ export function customUtilitiesPlugin({ addUtilities, theme }) {
107
107
  * // fontFamily: 'var(--heading-font-family)',
108
108
  * // fontStyle: 'var(--heading-font-style)',
109
109
  * // letterSpacing: 'var(--heading-letter-spacing)',
110
- * // color: 'rgb(var(--heading-font-color))'
110
+ * // color: 'var(--heading-font-color)'
111
111
  * // }
112
112
  * // }
113
113
  */
@@ -8,5 +8,5 @@ export function setMetaThemeColor() {
8
8
  .getPropertyValue('--color-surface-950')
9
9
  .trim();
10
10
 
11
- themeStore.setMetaThemeColor(`rgb(${surfaceColor})`);
11
+ themeStore.setMetaThemeColor(surfaceColor);
12
12
  }
@@ -6,15 +6,11 @@ export class InternalEventOrLogError extends Error {
6
6
  }
7
7
  export class DetailedError extends Error {
8
8
  /**
9
- * @param {string} message - Error message
10
- * @param {string|{[key: string]: any}|null} [details] - Additional details
11
- * @param {Error|string} [cause] - Original 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: string, details?: string | {
14
- [key: string]: any;
15
- } | null, cause?: Error | string);
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 - Error message
11
- * @param {string|{[key: string]: any}|null} [details] - Additional details
12
- * @param {Error|string} [cause] - Original 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
- this.cause = cause;
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
  }
@@ -0,0 +1,5 @@
1
+ declare const _default: {};
2
+ export default _default;
3
+ export type ErrorDetails = string | {
4
+ [key: string]: any;
5
+ } | null;
@@ -0,0 +1,3 @@
1
+ /** @typedef {string|{[key: string]: any}|null} ErrorDetails */
2
+
3
+ export default {};
@@ -1,3 +1,4 @@
1
1
  export * from "./data/typedef.js";
2
+ export * from "./errors/typedef.js";
2
3
  declare const _default: {};
3
4
  export default _default;
@@ -1,3 +1,4 @@
1
1
  export * from './data/typedef.js';
2
+ export * from './errors/typedef.js';
2
3
 
3
4
  export default {};
@@ -6,6 +6,13 @@ export class LoggerError extends Error
6
6
  constructor( originalError ) {
7
7
  super('LoggerError');
8
8
  this.name = 'LoggerError';
9
- this.cause = originalError;
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
- * originalErrorOrMessage
54
- * @param {Error|Object} [originalErrorOrDetails]
53
+ * errorOrMessage
54
+ *
55
+ * @param {Error|ErrorDetails} [errorOrDetails]
55
56
  * Error object (when first param is string) or details object
56
- * @param {Object} [details]
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(originalErrorOrMessage: Error | ErrorEvent | PromiseRejectionEvent | string, originalErrorOrDetails?: Error | any, details?: any, ...args: any[]): boolean;
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/index.js';
51
+ import { toArray } from '../../../util/array.js';
52
+ import { exportNotNullish } from '../../../util/object.js';
50
53
 
51
- import {
52
- castErrorEventToDetailedError,
53
- castPromiseRejectionToDetailedError
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
- * originalErrorOrMessage
138
- * @param {Error|Object} [originalErrorOrDetails]
140
+ * errorOrMessage
141
+ *
142
+ * @param {Error|ErrorDetails} [errorOrDetails]
139
143
  * Error object (when first param is string) or details object
140
- * @param {Object} [details]
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(originalErrorOrMessage, originalErrorOrDetails, details) {
146
- // Detection logic - set clear internal variables
147
- let message;
148
- let errorDetails;
149
- let cause;
150
+ error(errorOrMessage, errorOrDetails, details) {
151
+ let errorObject = null;
150
152
 
151
- // Handle browser ErrorEvent
152
- if (is.ErrorEvent(originalErrorOrMessage)) {
153
- const errorEvent = /** @type {ErrorEvent} */ (originalErrorOrMessage);
153
+ let message;
154
154
 
155
- message =
156
- errorEvent.error?.message || errorEvent.message || 'Unknown error';
155
+ if( typeof errorOrMessage === 'string' )
156
+ {
157
+ message = errorOrMessage;
158
+ }
157
159
 
158
- // Use provided details or auto-generate from event
160
+ if (message) {
159
161
 
160
- errorDetails = originalErrorOrDetails || {
161
- filename: errorEvent.filename,
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
- cause = errorEvent.error;
168
- }
169
- // Handle browser PromiseRejectionEvent
170
- else if (is.PromiseRejectionEvent(originalErrorOrMessage)) {
171
- const promiseRejectionEvent =
172
- /** @type {PromiseRejectionEvent} */ (originalErrorOrMessage);
165
+ if (this.#isErrorLike(errorOrDetails)) {
166
+ let errorLike = errorOrDetails;
167
+ errorObject = this.#toError(errorLike);
168
+ }
169
+ } else {
173
170
 
174
- const reason = promiseRejectionEvent.reason;
171
+ // First param is not an (not empty) string
172
+ // => First param should be the error
175
173
 
176
- if (reason instanceof Error) {
177
- message = reason.message;
178
- cause = reason;
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
- // Use provided details or auto-generate from event
185
- errorDetails = originalErrorOrDetails || {
186
- type: 'PromiseRejectionEvent',
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
- // Handle regular Error
191
- else if (originalErrorOrMessage instanceof Error) {
192
- message = originalErrorOrMessage.message;
193
- errorDetails = originalErrorOrDetails || null;
194
- cause = originalErrorOrMessage;
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
- // Handle string + Error pattern
197
- else if (
198
- typeof originalErrorOrMessage === 'string' &&
199
- originalErrorOrDetails instanceof Error
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
- // Handle string only
206
- else if (typeof originalErrorOrMessage === 'string') {
207
- message = originalErrorOrMessage;
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
- message = 'Invalid parameters supplied to Logger.error';
214
- errorDetails = toArray(arguments);
215
- cause = null;
216
- }
201
+ // Missing error like object
202
+ // => invalid parameters supplied to logger.error
217
203
 
218
- // Create consistent DetailedError for all cases
219
- const detailedError = new DetailedError(message, errorDetails, cause);
204
+ const detailedError = new DetailedError(
205
+ 'Missing error like object in Logger.error parameters',
206
+ toArray(arguments)
207
+ );
220
208
 
221
- return this.#log(ERROR, detailedError.message, detailedError);
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} originalError
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, originalError: Error | string, details?: 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} originalError
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, originalError, details ) {
17
+ export function rethrow(message, originalErrorOrMessage, details ) {
18
18
  let error;
19
19
 
20
- if (!(originalError instanceof Error)) {
20
+ if ( typeof originalErrorOrMessage === 'string' ) {
21
21
  // Convert non-Error values to Error objects
22
- error = new Error(String(originalError));
22
+ error = new Error( originalErrorOrMessage );
23
+ }
24
+ else {
25
+ error = originalErrorOrMessage;
23
26
  }
24
27
 
25
- throw new DetailedError(message, details, error ?? originalError );
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 exportNotNull(obj: object, onlyKeys?: string[]): object;
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 exportNotNull(obj, onlyKeys) {
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.4.16",
3
+ "version": "0.4.18",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"
@@ -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
-