fix: improved error handling with formatted json error messages

This commit is contained in:
kangfenmao 2024-12-03 20:42:13 +08:00
parent 73973ecb7f
commit a71782abb6

View File

@ -74,11 +74,7 @@ export async function fetchChatCompletion({
}
} catch (error: any) {
message.status = 'error'
try {
message.content = '```json\n' + JSON.stringify(error, null, 2) + '\n```'
} catch (e) {
message.content = 'Error: ' + error.message
}
message.content = formatErrorMessage(error)
}
timer && clearInterval(timer)
@ -231,3 +227,15 @@ export async function fetchModels(provider: Provider) {
return []
}
}
function formatErrorMessage(error: any): string {
try {
return (
'```json\n' +
JSON.stringify(error?.response?.data || error?.response || error?.request || error, null, 2) +
'\n```'
)
} catch (e) {
return 'Error: ' + error.message
}
}