fix(MessageTools): improve error handling and logging in message preview rendering (#8453)

- Enhanced the rendering logic for message previews by adding a try-catch block to handle JSON parsing errors more gracefully.
- Updated the error handling to provide clearer error messages in the preview when exceptions occur.
- Added debug logging to track the rendering process of message content.
This commit is contained in:
SuYao 2025-07-24 15:17:01 +08:00 committed by GitHub
parent 06dd581fc3
commit 85347885bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -280,22 +280,37 @@ const MessageTools: FC<Props> = ({ block }) => {
if (!content) return null
try {
logger.debug(`renderPreview: ${content}`)
const parsedResult = JSON.parse(content)
switch (parsedResult.content[0]?.type) {
case 'text':
return (
<CollapsedContent
isExpanded={true}
resultString={JSON.stringify(JSON.parse(parsedResult.content[0].text), null, 2)}
/>
)
try {
return (
<CollapsedContent
isExpanded={true}
resultString={JSON.stringify(JSON.parse(parsedResult.content[0].text), null, 2)}
/>
)
} catch (e) {
return (
<CollapsedContent
isExpanded={true}
resultString={JSON.stringify(parsedResult.content[0].text, null, 2)}
/>
)
}
default:
return <CollapsedContent isExpanded={true} resultString={JSON.stringify(parsedResult, null, 2)} />
}
} catch (e) {
logger.error('failed to render the preview of mcp results:', e as Error)
return <CollapsedContent isExpanded={true} resultString={JSON.stringify(e, null, 2)} />
return (
<CollapsedContent
isExpanded={true}
resultString={e instanceof Error ? e.message : JSON.stringify(e, null, 2)}
/>
)
}
}