feat: enhance error handling for LLM API calls
Add LLM error alert functionality to display specific error messages based on API responses. Introduce new LlmErrorAlertType interface for structured error alerts. Update chat components to manage and display LLM error alerts effectively, improving user feedback during error scenarios.
This commit is contained in:
@@ -375,16 +375,34 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
|
||||
} catch (error: any) {
|
||||
logger.error(error);
|
||||
|
||||
const errorResponse = {
|
||||
error: true,
|
||||
message: error.message || 'An unexpected error occurred',
|
||||
statusCode: error.statusCode || 500,
|
||||
isRetryable: error.isRetryable !== false, // Default to retryable unless explicitly false
|
||||
provider: error.provider || 'unknown',
|
||||
};
|
||||
|
||||
if (error.message?.includes('API key')) {
|
||||
throw new Response('Invalid or missing API key', {
|
||||
status: 401,
|
||||
statusText: 'Unauthorized',
|
||||
});
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
...errorResponse,
|
||||
message: 'Invalid or missing API key',
|
||||
statusCode: 401,
|
||||
isRetryable: false,
|
||||
}),
|
||||
{
|
||||
status: 401,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
statusText: 'Unauthorized',
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
throw new Response(null, {
|
||||
status: 500,
|
||||
statusText: 'Internal Server Error',
|
||||
return new Response(JSON.stringify(errorResponse), {
|
||||
status: errorResponse.statusCode,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
statusText: 'Error',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,16 +139,34 @@ async function llmCallAction({ context, request }: ActionFunctionArgs) {
|
||||
} catch (error: unknown) {
|
||||
console.log(error);
|
||||
|
||||
const errorResponse = {
|
||||
error: true,
|
||||
message: error instanceof Error ? error.message : 'An unexpected error occurred',
|
||||
statusCode: (error as any).statusCode || 500,
|
||||
isRetryable: (error as any).isRetryable !== false,
|
||||
provider: (error as any).provider || 'unknown',
|
||||
};
|
||||
|
||||
if (error instanceof Error && error.message?.includes('API key')) {
|
||||
throw new Response('Invalid or missing API key', {
|
||||
status: 401,
|
||||
statusText: 'Unauthorized',
|
||||
});
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
...errorResponse,
|
||||
message: 'Invalid or missing API key',
|
||||
statusCode: 401,
|
||||
isRetryable: false,
|
||||
}),
|
||||
{
|
||||
status: 401,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
statusText: 'Unauthorized',
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
throw new Response(null, {
|
||||
status: 500,
|
||||
statusText: 'Internal Server Error',
|
||||
return new Response(JSON.stringify(errorResponse), {
|
||||
status: errorResponse.statusCode,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
statusText: 'Error',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user