fix: better handle for RetryError

This commit is contained in:
AnotiaWang
2025-03-09 22:50:55 +08:00
parent e5b3cdda76
commit 5e3eb5fc9b

View File

@ -1,25 +1,50 @@
import { APICallError } from 'ai' import { APICallError, RetryError } from 'ai'
function handleApiCallError(operation: string, error: APICallError) {
let message = error.message
if (error.statusCode) message += ` (${error.statusCode})`
if (error.cause) message += `\nCause: ${error.cause}`
if (error.responseBody) message += `\nResponse: ${error.responseBody}`
if (error.url) message += `\nURL: ${error.url}`
console.error(`[${operation}]`, error, {
statusCode: error.statusCode,
response: error.responseBody,
cause: error.cause,
stack: error.stack,
isRetryable: error.isRetryable,
url: error.url,
})
throw new Error(message)
}
function handleRetryError(operation: string, error: RetryError) {
if (APICallError.isInstance(error.lastError)) {
handleApiCallError(operation, error.lastError)
}
let message = error.message
if (error.cause) message += `\nCause: ${error.cause}`
if (error.stack) message += `\nStack: ${error.stack}`
if (error.reason) message += `\nReason: ${error.reason}`
console.error(`[${operation}]`, error, {
cause: error.cause,
stack: error.stack,
lastError: error.lastError,
reason: error.reason,
errors: error.errors,
})
throw new Error(message)
}
/** /**
* Parse an error thrown by the AI SDK, and re-throw it with a human-readable message * Parse an error thrown by the AI SDK, and re-throw it with a human-readable message
*/ */
export function throwAiError(operation: string, error: unknown) { export function throwAiError(operation: string, error: unknown) {
if (APICallError.isInstance(error)) { if (APICallError.isInstance(error)) {
let message = error.message handleApiCallError(operation, error)
if (error.statusCode) message += ` (${error.statusCode})` } else if (RetryError.isInstance(error)) {
if (error.cause) message += `\nCause: ${error.cause}` handleRetryError(operation, error)
if (error.responseBody) message += `\nResponse: ${error.responseBody}`
if (error.url) message += `\nURL: ${error.url}`
console.error(`[${operation}]`, error, {
statusCode: error.statusCode,
response: error.responseBody,
cause: error.cause,
stack: error.stack,
isRetryable: error.isRetryable,
url: error.url,
})
throw new Error(message)
} else { } else {
console.error(`[${operation}]`, error) console.error(`[${operation}]`, error)
} }