@likec4/log 1.55.1 → 1.57.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.
@@ -1,642 +0,0 @@
1
- import { t as isErrorInstance } from "./is-error-instance.mjs";
2
- import { t as isPlainObject } from "./is-plain-obj.mjs";
3
- const normalizeDescriptors = (error) => {
4
- CORE_ERROR_PROPS.forEach((propName) => {
5
- normalizeDescriptor$1(error, propName);
6
- });
7
- };
8
- const CORE_ERROR_PROPS = [
9
- "name",
10
- "message",
11
- "stack",
12
- "cause",
13
- "errors"
14
- ];
15
- const normalizeDescriptor$1 = (error, propName) => {
16
- const descriptor = getDescriptor(error, propName);
17
- if (descriptor === void 0) return;
18
- if (isReadonlyGetter(descriptor)) {
19
- setErrorProperty$1(error, propName, error[propName]);
20
- return;
21
- }
22
- if (isInvalidDescriptor(descriptor)) setErrorDescriptor(error, propName, descriptor);
23
- };
24
- const getDescriptor = (value, propName) => {
25
- const descriptor = Object.getOwnPropertyDescriptor(value, propName);
26
- if (descriptor !== void 0) return descriptor;
27
- const prototype = Object.getPrototypeOf(value);
28
- return prototype === null ? void 0 : getDescriptor(prototype, propName);
29
- };
30
- const isReadonlyGetter = ({ get, set }) => get !== void 0 && set === void 0;
31
- const isInvalidDescriptor = ({ enumerable, writable }) => enumerable || !writable;
32
- const setErrorProperty$1 = (error, propName, value) => {
33
- setErrorDescriptor(error, propName, { value });
34
- };
35
- const setErrorDescriptor = (error, propName, descriptor) => {
36
- Object.defineProperty(error, propName, {
37
- ...descriptor,
38
- ..."get" in descriptor || "set" in descriptor ? {} : { writable: true },
39
- enumerable: false,
40
- configurable: true
41
- });
42
- };
43
- const normalizeAggregate = (error, recurse) => {
44
- if (Array.isArray(error.errors)) setErrorProperty$1(error, "errors", error.errors.filter(isDefined).map(recurse).filter(Boolean));
45
- else if (isAggregateError(error)) setErrorProperty$1(error, "errors", []);
46
- else if (error.errors !== void 0) deleteAggregateErrors(error);
47
- };
48
- const isDefined = (error) => error !== void 0;
49
- const isAggregateError = (error) => "AggregateError" in globalThis && (error.name === "AggregateError" || error instanceof AggregateError);
50
- const deleteAggregateErrors = (error) => {
51
- delete error.errors;
52
- if (error.errors !== void 0) setErrorProperty$1(error, "errors", []);
53
- };
54
- const normalizeCause = (error, recurse) => {
55
- if (!("cause" in error)) return;
56
- const cause = error.cause === void 0 ? error.cause : recurse(error.cause);
57
- if (cause === void 0) delete error.cause;
58
- else setErrorProperty$1(error, "cause", cause);
59
- };
60
- const isNonModifiableError = (error) => !Object.isExtensible(error) || CORE_ERROR_PROPS.some((propName) => isNonConfigurableProp(error, propName) || isThrowingProp(error, propName));
61
- const isNonConfigurableProp = (error, propName) => {
62
- const descriptor = Object.getOwnPropertyDescriptor(error, propName);
63
- return descriptor !== void 0 && !descriptor.configurable;
64
- };
65
- const isThrowingProp = (error, propName) => {
66
- try {
67
- error[propName];
68
- return false;
69
- } catch {
70
- return true;
71
- }
72
- };
73
- const setStack = (error) => {
74
- setErrorProperty$1(error, "stack", getStack$3(error.message, error.name));
75
- };
76
- const getStack$3 = (message = "", name = "Error") => {
77
- const { stack } = new (getErrorClass(name))(message);
78
- return typeof stack === "string" && stack !== "" ? stack : `${name}: ${message}`;
79
- };
80
- const getErrorClass = (name) => {
81
- const descriptor = {
82
- value: name,
83
- enumerable: false,
84
- writable: true,
85
- configurable: true
86
- };
87
- const StackError = Object.defineProperty(class extends Error {}, "name", descriptor);
88
- Object.defineProperty(StackError.prototype, "name", descriptor);
89
- return StackError;
90
- };
91
- const copyObject = (object) => {
92
- const objectCopy = {};
93
- for (const propName of getPropsToCopy(object)) try {
94
- const value = object[propName];
95
- const { enumerable, configurable, writable = true } = getDescriptor(object, propName);
96
- Object.defineProperty(objectCopy, propName, {
97
- value,
98
- enumerable,
99
- configurable,
100
- writable
101
- });
102
- } catch {}
103
- return objectCopy;
104
- };
105
- const getPropsToCopy = (object) => {
106
- const propNames = getOwnKeys(object);
107
- for (const propName of CORE_ERROR_PROPS) if (isInheritedProp(object, propName)) propNames.push(propName);
108
- return propNames;
109
- };
110
- const getOwnKeys = (object) => {
111
- try {
112
- return Reflect.ownKeys(object);
113
- } catch {
114
- return [];
115
- }
116
- };
117
- const isInheritedProp = (object, propName) => {
118
- try {
119
- return propName in object && !Object.hasOwn(object, propName);
120
- } catch {
121
- return false;
122
- }
123
- };
124
- const objectifyError = (object) => {
125
- const { name, message, stack, cause, errors, ...objectA } = copyObject(object);
126
- const messageA = getMessage$1(message, objectA);
127
- const error = newError(name, messageA);
128
- if (message === messageA) assignObjectProps(error, objectA);
129
- Object.entries({
130
- name,
131
- stack,
132
- cause,
133
- errors
134
- }).forEach(([propName, propValue]) => {
135
- setNewErrorProperty(error, propName, propValue);
136
- });
137
- if (stack === void 0) setStack(error);
138
- return error;
139
- };
140
- const getMessage$1 = (message, object) => typeof message === "string" && message !== "" ? message : truncateMessage(safeJsonStringify(object));
141
- const safeJsonStringify = (object) => {
142
- try {
143
- return JSON.stringify(object);
144
- } catch {
145
- return safeStringify(object);
146
- }
147
- };
148
- const safeStringify = (object) => {
149
- try {
150
- return String(object);
151
- } catch {
152
- return "Invalid error";
153
- }
154
- };
155
- const truncateMessage = (message) => message.length < MESSAGE_MAX_SIZE ? message : `${message.slice(0, MESSAGE_MAX_SIZE)}...`;
156
- const MESSAGE_MAX_SIZE = 1e3;
157
- const newError = (name, message) => {
158
- if (name === "AggregateError" && "AggregateError" in globalThis) return new AggregateError([], message);
159
- if (name in NATIVE_ERRORS) return new NATIVE_ERRORS[name](message);
160
- return new Error(message);
161
- };
162
- const NATIVE_ERRORS = {
163
- Error,
164
- ReferenceError,
165
- TypeError,
166
- SyntaxError,
167
- RangeError,
168
- URIError,
169
- EvalError
170
- };
171
- const assignObjectProps = (error, object) => {
172
- for (const propName in object) if (!(propName in error)) error[propName] = object[propName];
173
- };
174
- const setNewErrorProperty = (error, propName, propValue) => {
175
- if (propValue !== void 0) setErrorProperty$1(error, propName, propValue);
176
- };
177
- const stringifyError = (value) => {
178
- try {
179
- const error = new Error(String(value));
180
- setStack(error);
181
- return error;
182
- } catch (error_) {
183
- return error_;
184
- }
185
- };
186
- const { toString: objectToString } = Object.prototype;
187
- const createError = (value) => {
188
- if (isErrorPlainObj(value)) return objectifyError(value);
189
- if (!isErrorInstance(value)) return stringifyError(value);
190
- if (isInvalidError(value)) return objectifyError(value);
191
- return value;
192
- };
193
- const isErrorPlainObj = (value) => {
194
- try {
195
- return isPlainObject(value);
196
- } catch {
197
- return false;
198
- }
199
- };
200
- const isInvalidError = (value) => isProxy(value) || isNonModifiableError(value) || hasInvalidConstructor(value);
201
- const isProxy = (value) => {
202
- try {
203
- return objectToString.call(value) === "[object Object]";
204
- } catch {
205
- return true;
206
- }
207
- };
208
- const hasInvalidConstructor = (error) => typeof error.constructor !== "function" || typeof error.constructor.name !== "string" || error.constructor.name === "" || error.constructor.prototype !== Object.getPrototypeOf(error);
209
- const normalizeException = (error, { shallow = false } = {}) => recurseException(error, [], shallow);
210
- const recurseException = (error, parents, shallow) => {
211
- if (parents.includes(error)) return;
212
- const recurse = shallow ? identity : (innerError) => recurseException(innerError, [...parents, error], shallow);
213
- const errorA = createError(error);
214
- normalizeProps(errorA, recurse);
215
- return errorA;
216
- };
217
- const identity = (error) => error;
218
- const normalizeProps = (error, recurse) => {
219
- normalizeName(error);
220
- normalizeMessage(error);
221
- normalizeStack(error);
222
- normalizeCause(error, recurse);
223
- normalizeAggregate(error, recurse);
224
- normalizeDescriptors(error);
225
- };
226
- const normalizeName = (error) => {
227
- if (isDefinedString$1(error.name)) return;
228
- const prototypeName = Object.getPrototypeOf(error).name;
229
- setErrorProperty$1(error, "name", isDefinedString$1(prototypeName) ? prototypeName : error.constructor.name);
230
- };
231
- const normalizeMessage = (error) => {
232
- if (!isDefinedString$1(error.message)) setErrorProperty$1(error, "message", "");
233
- };
234
- const normalizeStack = (error) => {
235
- if (!isDefinedString$1(error.stack)) setStack(error);
236
- };
237
- const isDefinedString$1 = (value) => typeof value === "string" && value !== "";
238
- const normalizeArgs$1 = (error, ErrorClass, currentName = error.name) => {
239
- validateErrorClass(ErrorClass);
240
- if (typeof currentName !== "string") throw new TypeError(`currentName must be a string: ${currentName}`);
241
- return currentName;
242
- };
243
- const validateErrorClass = (ErrorClass) => {
244
- if (!isClass(ErrorClass)) throw new TypeError(`ErrorClass must be a class: ${ErrorClass}`);
245
- if (!isErrorClass(ErrorClass.prototype)) throw new TypeError(`ErrorClass must inherit from Error: ${ErrorClass}`);
246
- if (!hasConstructor(ErrorClass)) throw new TypeError(`ErrorClass must be have a valid constructor: ${ErrorClass}`);
247
- };
248
- const isClass = (ErrorClass) => typeof ErrorClass === "function" && typeof ErrorClass.prototype === "object" && ErrorClass.prototype !== null;
249
- const isErrorClass = (prototype) => prototype !== null && (prototype.name === "Error" || isErrorClass(Object.getPrototypeOf(prototype)));
250
- const hasConstructor = (ErrorClass) => typeof ErrorClass.prototype.constructor === "function";
251
- const setNonEnumProp$1 = (error, propName, value) => {
252
- Object.defineProperty(error, propName, {
253
- value,
254
- enumerable: false,
255
- writable: true,
256
- configurable: true
257
- });
258
- };
259
- const updatePrototype = (error, ErrorClass) => {
260
- if (Object.getPrototypeOf(error) === ErrorClass.prototype) return;
261
- setPrototype(error, ErrorClass);
262
- deleteOwnProperty(error, "constructor");
263
- fixName(error, ErrorClass);
264
- };
265
- const setPrototype = (error, ErrorClass) => {
266
- Object.setPrototypeOf(error, ErrorClass.prototype);
267
- };
268
- const fixName = (error, ErrorClass) => {
269
- deleteOwnProperty(error, "name");
270
- const prototypeName = getClassName(ErrorClass.prototype);
271
- if (error.name !== prototypeName) setNonEnumProp$1(error, "name", prototypeName);
272
- };
273
- const getClassName = (prototype) => getPrototypeName(prototype) ?? getConstructorName(prototype) ?? getClassName(Object.getPrototypeOf(prototype));
274
- const getPrototypeName = (prototype) => Object.hasOwn(prototype, "name") && isDefinedString(prototype.name) ? prototype.name : void 0;
275
- const getConstructorName = (prototype) => typeof prototype.constructor === "function" && isDefinedString(prototype.constructor.name) ? prototype.constructor.name : void 0;
276
- const isDefinedString = (value) => typeof value === "string" && value !== "";
277
- const deleteOwnProperty = (error, propName) => {
278
- if (Object.hasOwn(error, propName)) delete error[propName];
279
- };
280
- const updateStack$1 = (error, currentName) => {
281
- if (!shouldUpdateStack(error, currentName)) return;
282
- setNonEnumProp$1(error, "stack", getStack$2(error, currentName));
283
- };
284
- const shouldUpdateStack = (error, currentName) => currentName !== error.name && currentName !== "" && error.stack.includes(currentName) && stackIncludesName();
285
- const stackIncludesName = () => {
286
- class StackError extends Error {}
287
- const descriptor = {
288
- value: EXAMPLE_NAME,
289
- enumerable: false,
290
- writable: true,
291
- configurable: true
292
- };
293
- Object.defineProperty(StackError, "name", descriptor);
294
- Object.defineProperty(StackError.prototype, "name", descriptor);
295
- const { stack } = new StackError("");
296
- return typeof stack === "string" && stack.includes(EXAMPLE_NAME);
297
- };
298
- const EXAMPLE_NAME = "SetErrorClassError";
299
- const getStack$2 = ({ name, stack }, currentName) => {
300
- if (stack.startsWith(`${currentName}: `)) return stack.replace(currentName, name);
301
- const [fromA, to] = getReplacers$1(currentName, name).find(([from]) => stack.includes(from));
302
- return stack.replace(fromA, to);
303
- };
304
- const getReplacers$1 = (currentName, newName) => [
305
- [`\n${currentName}: `, `\n${newName}: `],
306
- [`${currentName}: `, `${newName}: `],
307
- [`${currentName} `, `${newName} `],
308
- [currentName, newName]
309
- ];
310
- const setErrorClass = (error, ErrorClass, currentName) => {
311
- const errorA = normalizeException(error);
312
- const currentNameA = normalizeArgs$1(errorA, ErrorClass, currentName);
313
- updatePrototype(errorA, ErrorClass);
314
- updateStack$1(errorA, currentNameA);
315
- return errorA;
316
- };
317
- const mergeDescriptors = (newDescriptor, currentDescriptor) => currentDescriptor.configurable === false ? mergeNonConfig(newDescriptor, currentDescriptor) : mergeConfig(newDescriptor, currentDescriptor);
318
- const mergeNonConfig = (newDescriptor, currentDescriptor) => ({
319
- ...currentDescriptor,
320
- ...getNonConfigWritable(newDescriptor, currentDescriptor),
321
- ...getNonConfigValue(newDescriptor, currentDescriptor)
322
- });
323
- const getNonConfigWritable = (newDescriptor, currentDescriptor) => currentDescriptor.writable === true && newDescriptor.writable === false ? { writable: false } : {};
324
- const getNonConfigValue = (newDescriptor, currentDescriptor) => newDescriptor.hasValue && "value" in currentDescriptor && currentDescriptor.writable === true ? { value: newDescriptor.value } : {};
325
- const mergeConfig = (newDescriptor, currentDescriptor) => {
326
- const enumerable = mergeDescriptor(newDescriptor.enumerable, currentDescriptor.enumerable, true);
327
- const writable = mergeDescriptor(newDescriptor.writable, currentDescriptor.writable, true);
328
- const configurable = mergeDescriptor(newDescriptor.configurable, currentDescriptor.configurable, true);
329
- return {
330
- ...mergeValue(newDescriptor, currentDescriptor, writable),
331
- enumerable,
332
- configurable
333
- };
334
- };
335
- const mergeValue = (newDescriptor, currentDescriptor, writable) => {
336
- if (newDescriptor.hasValue) return {
337
- value: newDescriptor.value,
338
- writable
339
- };
340
- if (!hasGetSet(newDescriptor) && !hasGetSet(currentDescriptor)) return {
341
- value: currentDescriptor.value,
342
- writable
343
- };
344
- return {
345
- get: mergeDescriptor(newDescriptor.get, currentDescriptor.get),
346
- set: mergeDescriptor(newDescriptor.set, currentDescriptor.set)
347
- };
348
- };
349
- const hasGetSet = ({ get, set }) => get !== void 0 || set !== void 0;
350
- const mergeDescriptor = (newValue, currentValue, defaultValue) => newValue ?? currentValue ?? defaultValue;
351
- const normalizeInput = (input, key, newDescriptor) => {
352
- if (!isAnyObj(input)) throw new TypeError(`Argument must be an object: ${input}`);
353
- if (!isValidKey(key)) throw new TypeError(`Property key must be a string, a symbol or an integer: ${key}`);
354
- return normalizeDescriptor(newDescriptor);
355
- };
356
- const isAnyObj = (value) => typeof value === "object" && value !== null;
357
- const isValidKey = (key) => {
358
- const type = typeof key;
359
- return type === "string" || type === "symbol" || type === "number";
360
- };
361
- const normalizeDescriptor = (newDescriptor) => {
362
- if (!isPlainObject(newDescriptor)) throw new TypeError(`Descriptor must be a plain object: ${newDescriptor}`);
363
- const { enumerable, writable, configurable, value, get, set, ...unknownProps } = newDescriptor;
364
- const hasValue = "value" in newDescriptor;
365
- validateDescriptor({
366
- enumerable,
367
- writable,
368
- configurable,
369
- get,
370
- set,
371
- unknownProps,
372
- hasValue
373
- });
374
- return {
375
- enumerable,
376
- writable,
377
- configurable,
378
- value,
379
- get,
380
- set,
381
- hasValue
382
- };
383
- };
384
- const validateDescriptor = ({ enumerable, writable, configurable, get, set, unknownProps, hasValue }) => {
385
- validateGetSet(hasValue, get, "get");
386
- validateGetSet(hasValue, set, "set");
387
- validateBoolean(enumerable, "enumerable");
388
- validateBoolean(writable, "writable");
389
- validateBoolean(configurable, "configurable");
390
- validateUnknownProps(unknownProps);
391
- };
392
- const validateGetSet = (hasValue, getSet, propName) => {
393
- validateFunction(getSet, propName);
394
- if (hasValue && getSet !== void 0) throw new TypeError(`Descriptor property "value" and "${propName}" must not both be defined: ${getSet}`);
395
- };
396
- const validateFunction = (propValue, propName) => {
397
- if (propValue !== void 0 && typeof propValue !== "function") throw new TypeError(`Descriptor property "${propName}" must be a function: ${propValue}`);
398
- };
399
- const validateBoolean = (propValue, propName) => {
400
- if (propValue !== void 0 && typeof propValue !== "boolean") throw new TypeError(`Descriptor property "${propName}" must be a boolean: ${propValue}`);
401
- };
402
- const validateUnknownProps = (unknownProps) => {
403
- const [unknownProp] = Object.keys(unknownProps);
404
- if (unknownProp !== void 0) throw new TypeError(`Unknown descriptor property "${unknownProp}": ${unknownProps[unknownProp]}`);
405
- };
406
- const redefineProperty = (input, key, newDescriptor) => {
407
- setProperty(input, key, mergeDescriptors(normalizeInput(input, key, newDescriptor), getCurrentDescriptor(input, key)));
408
- return input;
409
- };
410
- const getCurrentDescriptor = (input, key) => {
411
- const descriptor = Object.getOwnPropertyDescriptor(input, key);
412
- if (descriptor !== void 0) return descriptor;
413
- const prototype = Object.getPrototypeOf(input);
414
- return prototype === null ? {} : getCurrentDescriptor(prototype, key);
415
- };
416
- const setProperty = (input, key, finalDescriptor) => {
417
- try {
418
- Object.defineProperty(input, key, finalDescriptor);
419
- } catch {}
420
- };
421
- const assignProp = (error, propName, propValue) => {
422
- if (propValue !== void 0) return setProp(error, propName, propValue);
423
- try {
424
- delete error[propName];
425
- } catch {}
426
- if (error[propName] !== void 0) return setProp(error, propName);
427
- };
428
- const setProp = (error, propName, propValue) => {
429
- redefineProperty(error, propName, {
430
- value: propValue,
431
- ...getNonEnum(propName)
432
- });
433
- };
434
- const getNonEnum = (propName) => typeof propName === "string" && propName.startsWith("_") ? { enumerable: false } : {};
435
- const normalizeOptions = (error, props, opts = {}) => {
436
- validateErrorOrObject(error, "First argument");
437
- validateErrorOrObject(props, "Second argument");
438
- if (!isPlainObject(opts)) throw new TypeError(`Options must be a plain object: ${opts}`);
439
- const { soft = false } = opts;
440
- if (typeof soft !== "boolean") throw new TypeError(`Option "soft" must be a boolean: ${soft}`);
441
- return { soft };
442
- };
443
- const validateErrorOrObject = (value, prefix) => {
444
- if (value === void 0) throw new TypeError(`${prefix} is required.`);
445
- if (!isErrorOrObject(value)) throw new TypeError(`${prefix} must be a plain object or an error: ${value}`);
446
- };
447
- const isErrorOrObject = (value) => isPlainObject(value) || isErrorInstance(value);
448
- const shouldSkipProp = ({ error, props, propName, soft }) => isIgnoredPropName(propName) || !isEnum.call(props, propName) || soft && error[propName] !== void 0;
449
- const isIgnoredPropName = (propName) => propName in CHECK_ERROR || IGNORED_PROPS.has(propName);
450
- const CHECK_ERROR = /* @__PURE__ */ new Error("check");
451
- const IGNORED_PROPS = new Set([
452
- "prototype",
453
- "errors",
454
- "cause"
455
- ]);
456
- const { propertyIsEnumerable: isEnum } = Object.prototype;
457
- const setErrorProps = (error, props, opts) => {
458
- const { soft } = normalizeOptions(error, props, opts);
459
- for (const propName of Reflect.ownKeys(props)) setErrorProp({
460
- error,
461
- props,
462
- propName,
463
- soft
464
- });
465
- return error;
466
- };
467
- const setErrorProp = ({ error, props, propName, soft }) => {
468
- if (!shouldSkipProp({
469
- error,
470
- props,
471
- propName,
472
- soft
473
- })) assignProp(error, propName, props[propName]);
474
- };
475
- const setErrorProperty = (error, propName, value) => {
476
- Object.defineProperty(error, propName, {
477
- value,
478
- writable: true,
479
- enumerable: false,
480
- configurable: true
481
- });
482
- };
483
- const mergeAggregateCauses = (parent, recurse) => {
484
- if (parent.errors === void 0) return;
485
- setErrorProperty(parent, "errors", parent.errors.map((error) => recurse(error).error).filter(Boolean));
486
- };
487
- const mergeAggregateErrors = ({ target, source, parent, child }) => {
488
- if (!hasErrors(target)) {
489
- mergeSourceErrors(target, source);
490
- return;
491
- }
492
- if (hasErrors(source)) setErrorProperty(target, "errors", [...child.errors, ...parent.errors]);
493
- };
494
- const mergeSourceErrors = (target, source) => {
495
- if (source.errors !== void 0) setErrorProperty(target, "errors", source.errors);
496
- };
497
- const hasErrors = (targetOrSource) => targetOrSource.errors !== void 0 && targetOrSource.errors.length !== 0;
498
- const normalizeArgs = (error, newMessage, currentMessage = error.message) => {
499
- if (typeof newMessage !== "string") throw new TypeError(`newMessage must be a string: ${newMessage}`);
500
- if (typeof currentMessage !== "string") throw new TypeError(`currentMessage must be a string: ${currentMessage}`);
501
- return currentMessage;
502
- };
503
- const getStack$1 = ({ name, stack }, newMessage, currentMessage) => currentMessage !== "" && stack.includes(currentMessage) ? replaceMessage({
504
- name,
505
- stack,
506
- newMessage,
507
- currentMessage
508
- }) : insertMessage(name, stack, newMessage);
509
- const replaceMessage = ({ name, stack, newMessage, currentMessage }) => {
510
- const [fromA, to] = getReplacers(name, newMessage, currentMessage).find(([from]) => stack.includes(from));
511
- return stack.replace(fromA, to);
512
- };
513
- const getReplacers = (name, newMessage, currentMessage) => [
514
- [`${name}: ${currentMessage}`, `${name}: ${newMessage}`],
515
- [`: ${currentMessage}`, `: ${newMessage}`],
516
- [`\n${currentMessage}`, `\n${newMessage}`],
517
- [` ${currentMessage}`, ` ${newMessage}`],
518
- [currentMessage, newMessage]
519
- ];
520
- const insertMessage = (name, stack, newMessage) => {
521
- const nameAndColon = `${name}: `;
522
- const newMessageA = newMessage.trimEnd();
523
- if (stack === name || stack.startsWith(`${name}\n`)) return stack.replace(name, `${nameAndColon}${newMessageA}`);
524
- return stack.startsWith(nameAndColon) ? stack.replace(nameAndColon, `${nameAndColon}${newMessageA}\n`) : `${nameAndColon}${newMessageA}\n${stack}`;
525
- };
526
- const setErrorMessage = (error, newMessage, currentMessage) => {
527
- const errorA = normalizeException(error);
528
- const currentMessageA = normalizeArgs(errorA, newMessage, currentMessage);
529
- setNonEnumProp(errorA, "message", newMessage);
530
- updateStack(errorA, newMessage, currentMessageA);
531
- return errorA;
532
- };
533
- const updateStack = (error, newMessage, currentMessage) => {
534
- if (newMessage === currentMessage || !stackIncludesMessage()) return;
535
- setNonEnumProp(error, "stack", getStack$1(error, newMessage, currentMessage));
536
- };
537
- const stackIncludesMessage = () => {
538
- const { stack } = new Error(EXAMPLE_MESSAGE);
539
- return typeof stack === "string" && stack.includes(EXAMPLE_MESSAGE);
540
- };
541
- const EXAMPLE_MESSAGE = "set-error-message test message";
542
- const setNonEnumProp = (error, propName, value) => {
543
- Object.defineProperty(error, propName, {
544
- value,
545
- enumerable: false,
546
- writable: true,
547
- configurable: true
548
- });
549
- };
550
- const wrapErrorMessage = (error, newMessage, oldMessage) => {
551
- if (typeof newMessage !== "string") throw new TypeError(`Second argument must be a message string: ${newMessage}`);
552
- const errorA = normalizeException(error);
553
- return setErrorMessage(errorA, getMessage(newMessage, errorA.message), oldMessage);
554
- };
555
- const getMessage = (rawNewMessage, rawCurrentMessage) => {
556
- const newMessage = rawNewMessage.trim();
557
- const currentMessage = rawCurrentMessage.trim();
558
- if (newMessage === "") return currentMessage;
559
- if (currentMessage === "") return newMessage;
560
- return concatMessages(newMessage, currentMessage, rawNewMessage);
561
- };
562
- const concatMessages = (newMessage, currentMessage, rawNewMessage) => {
563
- if (!newMessage.endsWith(PREPEND_CHAR)) return `${currentMessage}\n${newMessage}`;
564
- return rawNewMessage.endsWith(PREPEND_NEWLINE_CHAR) ? `${newMessage}\n${currentMessage}` : `${newMessage} ${currentMessage}`;
565
- };
566
- const PREPEND_CHAR = ":";
567
- const PREPEND_NEWLINE_CHAR = "\n";
568
- const mergeMessage = ({ parent, child, target, stackError }) => {
569
- const parentMessage = parent.message;
570
- const stackErrorMessage = stackError.message;
571
- target.message = child.message;
572
- return wrapErrorMessage(target, parentMessage, stackErrorMessage);
573
- };
574
- const hasStack = (error, stack) => getStack(error) === stack;
575
- const getStack = (error) => typeof error === "object" && error !== null ? error.stack : void 0;
576
- const mergeStack = ({ wrap, target, source, childHasStack }) => {
577
- if (wrap === childHasStack) return target;
578
- setErrorProperty(target, "stack", source.stack);
579
- return source;
580
- };
581
- const getWrap = (parent) => {
582
- const { wrap, name } = parent;
583
- if (typeof wrap !== "boolean") return name === "Error";
584
- if (Object.hasOwn(parent, "wrap")) delete parent.wrap;
585
- return wrap;
586
- };
587
- const mergeErrorCause = (error) => mergeError(error, []).error;
588
- const mergeError = (error, parents) => {
589
- if (parents.includes(error)) return {};
590
- const recurse = (innerError) => mergeError(innerError, [...parents, error]);
591
- const stack = getStack(error);
592
- const errorA = normalizeException(error, { shallow: true });
593
- const parentHasStack = hasStack(errorA, stack);
594
- mergeAggregateCauses(errorA, recurse);
595
- const { parent: errorB, childHasStack } = mergeCause(errorA, recurse);
596
- return {
597
- error: errorB,
598
- errorHasStack: parentHasStack || childHasStack
599
- };
600
- };
601
- const mergeCause = (parent, recurse) => {
602
- const wrap = getWrap(parent);
603
- if (parent.cause === void 0) return {
604
- parent,
605
- childHasStack: false
606
- };
607
- const { error: child, errorHasStack: childHasStack } = recurse(parent.cause);
608
- delete parent.cause;
609
- return {
610
- parent: mergeChild({
611
- parent,
612
- child,
613
- childHasStack,
614
- wrap
615
- }),
616
- childHasStack
617
- };
618
- };
619
- const mergeChild = ({ parent, child, childHasStack, wrap }) => {
620
- if (child === void 0) return parent;
621
- const [target, source] = wrap ? [child, parent] : [parent, child];
622
- const stackError = mergeStack({
623
- wrap,
624
- target,
625
- source,
626
- childHasStack
627
- });
628
- const targetB = mergeMessage({
629
- parent,
630
- child,
631
- target: setErrorClass(target, target.constructor, stackError.name),
632
- stackError
633
- });
634
- mergeAggregateErrors({
635
- target: targetB,
636
- source,
637
- parent,
638
- child
639
- });
640
- return setErrorProps(targetB, source, { soft: !wrap });
641
- };
642
- export { wrapErrorMessage as n, mergeErrorCause as t };
@@ -1,19 +0,0 @@
1
- function serializeValue(value, seen, trace, currentPath) {
2
- if (typeof value?.toJSON === "function") value = value.toJSON();
3
- if (!(value !== null && typeof value === "object")) return value;
4
- if (seen.has(value)) {
5
- if (!trace) return "[Circular]";
6
- const existingPath = seen.get(value);
7
- return `[Circular ${existingPath === "" ? "*" : `*${existingPath}`}]`;
8
- }
9
- seen.set(value, currentPath);
10
- const newValue = Array.isArray(value) ? [] : {};
11
- for (const [propertyKey, propertyValue] of Object.entries(value)) newValue[propertyKey] = serializeValue(propertyValue, seen, trace, currentPath === "" ? propertyKey : `${currentPath}.${propertyKey}`);
12
- seen.delete(value);
13
- return newValue;
14
- }
15
- function safeStringify(value, { indentation, trace } = {}) {
16
- const serializedValue = serializeValue(value, /* @__PURE__ */ new WeakMap(), trace, "");
17
- return JSON.stringify(serializedValue, void 0, indentation);
18
- }
19
- export { safeStringify as t };