MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / safeStringify

Function safeStringify

common/src/util/error.ts:360–380  ·  view source on GitHub ↗

* Safely stringify an object, handling circular references and large objects.

(value: unknown, maxLength = 10000)

Source from the content-addressed store, hash-verified

358 * Safely stringify an object, handling circular references and large objects.
359 */
360function 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
382export function getErrorObject(
383 error: unknown,

Callers 1

getErrorObjectFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected