MCPcopy
hub / github.com/zgsm-ai/costrict / extractMessageFromJsonPayload

Function extractMessageFromJsonPayload

packages/types/src/telemetry.ts:354–380  ·  view source on GitHub ↗
(message: string)

Source from the content-addressed store, hash-verified

352 * @returns The extracted message from the JSON payload, or undefined if not found
353 */
354export function extractMessageFromJsonPayload(message: string): string | undefined {
355 // Find the first occurrence of '{' which may indicate JSON content
356 const jsonStartIndex = message.indexOf("{")
357 if (jsonStartIndex === -1) {
358 return undefined
359 }
360
361 const potentialJson = message.slice(jsonStartIndex)
362
363 try {
364 const parsed = JSON.parse(potentialJson)
365
366 // Handle structure: {"error":{"message":"..."}} or {"error":{"code":"","message":"..."}}
367 if (parsed?.error?.message && typeof parsed.error.message === "string") {
368 return parsed.error.message
369 }
370
371 // Handle structure: {"message":"..."}
372 if (parsed?.message && typeof parsed.message === "string") {
373 return parsed.message
374 }
375 } catch {
376 // JSON parsing failed - not valid JSON
377 }
378
379 return undefined
380}
381
382/**
383 * Extracts the most descriptive error message from an error object.

Callers 2

getErrorMessageFunction · 0.85
telemetry.test.tsFile · 0.85

Calls 1

parseMethod · 0.80

Tested by

no test coverage detected