@hkdigital/lib-core 0.4.15 → 0.4.16

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.
@@ -36,3 +36,9 @@ export function sign(claims: import("./typedef.js").JwtPayload, secretOrPrivateK
36
36
  * @returns {import('./typedef.js').JwtPayload} claims - The decoded JWT payload
37
37
  */
38
38
  export function verify(token: string, secretOrPrivateKey: import("./typedef.js").Secret, options?: import("./typedef.js").VerifyOptions): import("./typedef.js").JwtPayload;
39
+ /**
40
+ * Casts jsonwebtoken library errors to internal error types
41
+ * @param {Error} error - The original jsonwebtoken error
42
+ * @returns {Error} - The corresponding internal error
43
+ */
44
+ export function castJwtError(error: Error): Error;
@@ -131,7 +131,7 @@ export function verify( token, secretOrPrivateKey, options=VERIFY_OPTIONS )
131
131
  * @param {Error} error - The original jsonwebtoken error
132
132
  * @returns {Error} - The corresponding internal error
133
133
  */
134
- function castJwtError(error) {
134
+ export function castJwtError(error) {
135
135
  if (error instanceof JwtTokenExpiredError) {
136
136
  return new TokenExpiredError(error.message, error.expiredAt, error);
137
137
  }
@@ -109,11 +109,9 @@ const logger = createClientLogger('client');
109
109
 
110
110
  /** @type {import('@sveltejs/kit').HandleClientError} */
111
111
  export function handleError({ error, event }) {
112
- logger.error('Client error occurred', {
113
- message: error.message,
114
- stack: error.stack,
115
- url: event.url?.pathname,
116
- userAgent: navigator.userAgent
112
+ logger.error(error, {
113
+ url: event.url?.pathname,
114
+ userAgent: navigator.userAgent
117
115
  });
118
116
  }
119
117
 
@@ -126,19 +124,12 @@ export function init() {
126
124
 
127
125
  // Log unhandled errors
128
126
  window.addEventListener('error', (event) => {
129
- logger.error('Unhandled error', {
130
- message: event.error?.message || event.message,
131
- filename: event.filename,
132
- lineno: event.lineno,
133
- colno: event.colno
134
- });
127
+ logger.error(event, { url: window.location.pathname });
135
128
  });
136
129
 
137
130
  // Log unhandled promise rejections
138
131
  window.addEventListener('unhandledrejection', (event) => {
139
- logger.error('Unhandled promise rejection', {
140
- reason: event.reason
141
- });
132
+ logger.error(event, { url: window.location.pathname });
142
133
  });
143
134
  }
144
135
 
@@ -49,12 +49,16 @@ export default class Logger extends EventEmitter {
49
49
  /**
50
50
  * Log an error message
51
51
  *
52
- * @param {Error|string} originalErrorOrMessage
53
- * @param {Error} [originalError]
52
+ * @param {Error|ErrorEvent|PromiseRejectionEvent|string}
53
+ * originalErrorOrMessage
54
+ * @param {Error|Object} [originalErrorOrDetails]
55
+ * Error object (when first param is string) or details object
56
+ * @param {Object} [details]
57
+ * Additional context details (when using string + error pattern)
54
58
  *
55
59
  * @returns {boolean} True if the log was emitted
56
60
  */
57
- error(originalErrorOrMessage: Error | string, originalError?: Error, ...args: any[]): boolean;
61
+ error(originalErrorOrMessage: Error | ErrorEvent | PromiseRejectionEvent | string, originalErrorOrDetails?: Error | any, details?: any, ...args: any[]): boolean;
58
62
  /**
59
63
  * Create a child logger with additional context
60
64
  *
@@ -48,6 +48,13 @@ import { LoggerError } from '../../errors.js';
48
48
 
49
49
  import { toArray } from '../../../util/array/index.js';
50
50
 
51
+ import {
52
+ castErrorEventToDetailedError,
53
+ castPromiseRejectionToDetailedError
54
+ } from './util.js';
55
+
56
+ import * as is from '../../../util/is.js';
57
+
51
58
  /**
52
59
  * Logger class for consistent logging
53
60
  * @extends EventEmitter
@@ -126,41 +133,92 @@ export default class Logger extends EventEmitter {
126
133
  /**
127
134
  * Log an error message
128
135
  *
129
- * @param {Error|string} originalErrorOrMessage
130
- * @param {Error} [originalError]
136
+ * @param {Error|ErrorEvent|PromiseRejectionEvent|string}
137
+ * originalErrorOrMessage
138
+ * @param {Error|Object} [originalErrorOrDetails]
139
+ * Error object (when first param is string) or details object
140
+ * @param {Object} [details]
141
+ * Additional context details (when using string + error pattern)
131
142
  *
132
143
  * @returns {boolean} True if the log was emitted
133
144
  */
134
- error(originalErrorOrMessage, originalError) {
145
+ error(originalErrorOrMessage, originalErrorOrDetails, details) {
146
+ // Detection logic - set clear internal variables
147
+ let message;
148
+ let errorDetails;
149
+ let cause;
150
+
151
+ // Handle browser ErrorEvent
152
+ if (is.ErrorEvent(originalErrorOrMessage)) {
153
+ const errorEvent = /** @type {ErrorEvent} */ (originalErrorOrMessage);
154
+
155
+ message =
156
+ errorEvent.error?.message || errorEvent.message || 'Unknown error';
157
+
158
+ // Use provided details or auto-generate from event
159
+
160
+ errorDetails = originalErrorOrDetails || {
161
+ filename: errorEvent.filename,
162
+ lineno: errorEvent.lineno,
163
+ colno: errorEvent.colno,
164
+ type: 'ErrorEvent'
165
+ };
135
166
 
136
- if( originalErrorOrMessage instanceof Error )
137
- {
138
- // params: {error} originalErrorOrMessage
139
- const loggerError = new LoggerError(originalErrorOrMessage);
167
+ cause = errorEvent.error;
168
+ }
169
+ // Handle browser PromiseRejectionEvent
170
+ else if (is.PromiseRejectionEvent(originalErrorOrMessage)) {
171
+ const promiseRejectionEvent =
172
+ /** @type {PromiseRejectionEvent} */ (originalErrorOrMessage);
173
+
174
+ const reason = promiseRejectionEvent.reason;
140
175
 
141
- const message = originalErrorOrMessage.message;
176
+ if (reason instanceof Error) {
177
+ message = reason.message;
178
+ cause = reason;
179
+ } else {
180
+ message = String(reason);
181
+ cause = null;
182
+ }
142
183
 
143
- return this.#log(ERROR, message, loggerError);
184
+ // Use provided details or auto-generate from event
185
+ errorDetails = originalErrorOrDetails || {
186
+ type: 'PromiseRejectionEvent',
187
+ reasonType: typeof reason
188
+ };
189
+ }
190
+ // Handle regular Error
191
+ else if (originalErrorOrMessage instanceof Error) {
192
+ message = originalErrorOrMessage.message;
193
+ errorDetails = originalErrorOrDetails || null;
194
+ cause = originalErrorOrMessage;
195
+ }
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;
144
204
  }
145
- else if( typeof originalErrorOrMessage === 'string' && originalError instanceof Error ) {
146
- // params: {string} message, {error} originalError
147
- const detailedError = new DetailedError(
148
- originalErrorOrMessage,
149
- null,
150
- originalError
151
- );
152
-
153
- return this.#log(ERROR, detailedError.message, detailedError);
205
+ // Handle string only
206
+ else if (typeof originalErrorOrMessage === 'string') {
207
+ message = originalErrorOrMessage;
208
+ errorDetails = originalErrorOrDetails || null;
209
+ cause = null;
154
210
  }
211
+ // Invalid parameters
155
212
  else {
156
- // wrong params
157
- const detailedError = new DetailedError(
158
- 'Invalid parameters supplied to Logger.error',
159
- toArray(arguments)
160
- );
161
-
162
- return this.#log(ERROR, detailedError.message, detailedError);
213
+ message = 'Invalid parameters supplied to Logger.error';
214
+ errorDetails = toArray(arguments);
215
+ cause = null;
163
216
  }
217
+
218
+ // Create consistent DetailedError for all cases
219
+ const detailedError = new DetailedError(message, errorDetails, cause);
220
+
221
+ return this.#log(ERROR, detailedError.message, detailedError);
164
222
  }
165
223
 
166
224
  /**
@@ -0,0 +1,13 @@
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';
@@ -0,0 +1,47 @@
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
+
@@ -104,7 +104,7 @@ Manages multiple services with dependency resolution and coordinated lifecycle o
104
104
  ### Usage
105
105
 
106
106
  ```javascript
107
- import { ServiceManager } from '$lib/services/index.js';
107
+ import { ServiceManager } from '$hklib-core/services/index.js';
108
108
  import DatabaseService from './services/DatabaseService.js';
109
109
  import AuthService from './services/AuthService.js';
110
110
 
@@ -197,4 +197,4 @@ Services include comprehensive test suites demonstrating:
197
197
  - Health monitoring
198
198
  - Event emission
199
199
 
200
- Run tests with your project's test command to ensure service reliability.
200
+ Run tests with your project's test command to ensure service reliability.
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Check if object is an ErrorEvent
3
+ *
4
+ * @param {any} obj
5
+ *
6
+ * @returns {boolean}
7
+ */
8
+ export function ErrorEvent(obj: any): boolean;
9
+ /**
10
+ * Check if object is a PromiseRejectionEvent
11
+ *
12
+ * @param {any} obj
13
+ *
14
+ * @returns {boolean}
15
+ */
16
+ export function PromiseRejectionEvent(obj: any): boolean;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Check if object is an ErrorEvent
3
+ *
4
+ * @param {any} obj
5
+ *
6
+ * @returns {boolean}
7
+ */
8
+ export function ErrorEvent(obj) {
9
+ return Boolean(obj &&
10
+ typeof obj === 'object' &&
11
+ obj.constructor?.name === 'ErrorEvent');
12
+ }
13
+
14
+ /**
15
+ * Check if object is a PromiseRejectionEvent
16
+ *
17
+ * @param {any} obj
18
+ *
19
+ * @returns {boolean}
20
+ */
21
+ export function PromiseRejectionEvent(obj) {
22
+ return Boolean(obj &&
23
+ typeof obj === 'object' &&
24
+ obj.constructor?.name === 'PromiseRejectionEvent');
25
+ }
@@ -68,3 +68,4 @@ export { argumentsCheck as arguments };
68
68
  * @returns {boolean} true if the value is an Arguments object
69
69
  */
70
70
  declare function argumentsCheck(value: any): boolean;
71
+ export { ErrorEvent, PromiseRejectionEvent } from "./events.js";
@@ -146,3 +146,5 @@ export function object(value) {
146
146
 
147
147
  return true;
148
148
  }
149
+
150
+ export { ErrorEvent, PromiseRejectionEvent } from './events.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.4.15",
3
+ "version": "0.4.16",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"