* Safely stringify an object, handling circular references and large objects.
(value: unknown, maxLength = 10000)
| 358 | * Safely stringify an object, handling circular references and large objects. |
| 359 | */ |
| 360 | function safeStringify(value: unknown, maxLength = 10000): string | undefined { |
| 361 | if (value === undefined || value === null) return undefined |
| 362 | if (typeof value === 'string') return value.slice(0, maxLength) |
| 363 | try { |
| 364 | const seen = new WeakSet() |
| 365 | const str = JSON.stringify( |
| 366 | value, |
| 367 | (_, val) => { |
| 368 | if (typeof val === 'object' && val !== null) { |
| 369 | if (seen.has(val)) return '[Circular]' |
| 370 | seen.add(val) |
| 371 | } |
| 372 | return val |
| 373 | }, |
| 374 | 2, |
| 375 | ) |
| 376 | return str?.slice(0, maxLength) |
| 377 | } catch { |
| 378 | return '[Unable to stringify]' |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | export function getErrorObject( |
| 383 | error: unknown, |