mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-08 06:19:05 +08:00
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:
parent
06dd581fc3
commit
85347885bd
@ -280,22 +280,37 @@ const MessageTools: FC<Props> = ({ block }) => {
|
|||||||
if (!content) return null
|
if (!content) return null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
logger.debug(`renderPreview: ${content}`)
|
||||||
const parsedResult = JSON.parse(content)
|
const parsedResult = JSON.parse(content)
|
||||||
switch (parsedResult.content[0]?.type) {
|
switch (parsedResult.content[0]?.type) {
|
||||||
case 'text':
|
case 'text':
|
||||||
return (
|
try {
|
||||||
<CollapsedContent
|
return (
|
||||||
isExpanded={true}
|
<CollapsedContent
|
||||||
resultString={JSON.stringify(JSON.parse(parsedResult.content[0].text), null, 2)}
|
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:
|
default:
|
||||||
return <CollapsedContent isExpanded={true} resultString={JSON.stringify(parsedResult, null, 2)} />
|
return <CollapsedContent isExpanded={true} resultString={JSON.stringify(parsedResult, null, 2)} />
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('failed to render the preview of mcp results:', e as Error)
|
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)}
|
||||||
|
/>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user