feat: improve error handling and formatting across providers
This commit is contained in:
parent
3d16c735d9
commit
a1b88758cc
@ -50,7 +50,7 @@ export default defineConfig({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
exclude: ['chunk-PZ64DZKH.js']
|
exclude: ['chunk-PZ64DZKH.js', 'chunk-JMKENWIY.js']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@ -225,7 +225,7 @@ export default class OpenAIProvider extends BaseProvider {
|
|||||||
onChunk({
|
onChunk({
|
||||||
text: delta?.content || '',
|
text: delta?.content || '',
|
||||||
// @ts-ignore key is not typed
|
// @ts-ignore key is not typed
|
||||||
reasoning_content: delta?.reasoning_content || delta.reasoning || '',
|
reasoning_content: delta?.reasoning_content || delta?.reasoning || '',
|
||||||
usage: chunk.usage,
|
usage: chunk.usage,
|
||||||
metrics: {
|
metrics: {
|
||||||
completion_tokens: chunk.usage?.completion_tokens,
|
completion_tokens: chunk.usage?.completion_tokens,
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import i18n from '@renderer/i18n'
|
|||||||
import store from '@renderer/store'
|
import store from '@renderer/store'
|
||||||
import { setGenerating } from '@renderer/store/runtime'
|
import { setGenerating } from '@renderer/store/runtime'
|
||||||
import { Assistant, Message, Model, Provider, Suggestion } from '@renderer/types'
|
import { Assistant, Message, Model, Provider, Suggestion } from '@renderer/types'
|
||||||
|
import { formatErrorMessage } from '@renderer/utils/error'
|
||||||
import { isEmpty } from 'lodash'
|
import { isEmpty } from 'lodash'
|
||||||
|
|
||||||
import AiProvider from '../providers/AiProvider'
|
import AiProvider from '../providers/AiProvider'
|
||||||
@ -241,11 +242,3 @@ export async function fetchModels(provider: Provider) {
|
|||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatErrorMessage(error: any): string {
|
|
||||||
try {
|
|
||||||
return '```json\n' + JSON.stringify(error, null, 2) + '\n```'
|
|
||||||
} catch (e) {
|
|
||||||
return 'Error: ' + error?.message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
41
src/renderer/src/utils/error.ts
Normal file
41
src/renderer/src/utils/error.ts
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
export function getErrorDetails(err: any, seen = new WeakSet()): any {
|
||||||
|
// Handle circular references
|
||||||
|
if (err === null || typeof err !== 'object' || seen.has(err)) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
seen.add(err)
|
||||||
|
const result: any = {}
|
||||||
|
|
||||||
|
// Get all enumerable properties, including those from the prototype chain
|
||||||
|
const allProps = new Set([...Object.getOwnPropertyNames(err), ...Object.keys(err)])
|
||||||
|
|
||||||
|
for (const prop of allProps) {
|
||||||
|
try {
|
||||||
|
const value = err[prop]
|
||||||
|
// Skip function properties
|
||||||
|
if (typeof value === 'function') continue
|
||||||
|
// Recursively process nested objects
|
||||||
|
result[prop] = getErrorDetails(value, seen)
|
||||||
|
} catch (e) {
|
||||||
|
result[prop] = '<Unable to access property>'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatErrorMessage(error: any): string {
|
||||||
|
console.error('Original error:', error)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const detailedError = getErrorDetails(error)
|
||||||
|
return '```json\n' + JSON.stringify(detailedError, null, 2) + '\n```'
|
||||||
|
} catch (e) {
|
||||||
|
try {
|
||||||
|
return '```\n' + String(error) + '\n```'
|
||||||
|
} catch {
|
||||||
|
return 'Error: Unable to format error message'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user