Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions packages/ai/src/prompt/wrap-gateway-error.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import {
GatewayAuthenticationError,
GatewayModelNotFoundError,
GatewayAuthenticationError
} from '@ai-sdk/gateway';
import { AISDKError } from '@ai-sdk/provider';

export function wrapGatewayError(error: unknown): unknown {
if (
GatewayAuthenticationError.isInstance(error) ||
GatewayModelNotFoundError.isInstance(error)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lars @mclenhard throwing the custom unauthenticated error for GatewayModelNotFoundError was incorrect. Do we want a custom error for when a model was not found? I don't think we need it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cause property was removed from the AISDKError constructor, breaking the error chain and losing important debugging information.

View Details
📝 Patch Details
diff --git a/packages/ai/src/prompt/wrap-gateway-error.ts b/packages/ai/src/prompt/wrap-gateway-error.ts
index 0417be108..4bb058e1e 100644
--- a/packages/ai/src/prompt/wrap-gateway-error.ts
+++ b/packages/ai/src/prompt/wrap-gateway-error.ts
@@ -7,6 +7,7 @@ export function wrapGatewayError(error: unknown): unknown {
       name: 'GatewayError',
       message:
         'Unauthenticated. Configure AI_GATEWAY_API_KEY or configure and use a provider module. Learn more: https://vercel.link/unauthenticated-ai-gateway-v6',
+      cause: error,
     });
   }
 

Analysis

Missing cause property breaks error chain in wrapGatewayError()

What fails: wrapGatewayError() in packages/ai/src/prompt/wrap-gateway-error.ts does not pass the cause property when creating the wrapped AISDKError, breaking the error causality chain for debugging and error diagnostics.

How to reproduce:

import { wrapGatewayError } from './packages/ai/src/prompt/wrap-gateway-error';
import { GatewayAuthenticationError } from '@ai-sdk/gateway';

const originalError = new GatewayAuthenticationError({
  message: 'Auth failed',
  statusCode: 401,
  cause: new Error('Network timeout')
});

const wrappedError = wrapGatewayError(originalError) as AISDKError;
console.log(wrappedError.cause); // undefined - original error is lost

Result: The cause property is undefined on the wrapped error, losing reference to the original GatewayAuthenticationError and preventing access to underlying error context (e.g., network errors, status codes from the authentication attempt).

Expected: The cause property should contain the original GatewayAuthenticationError instance, preserving the error chain for proper error diagnostics and monitoring. The AISDKError class supports optional cause parameter as defined in packages/provider/src/errors/ai-sdk-error.ts. All existing tests pass when cause: error is included.

Reference: AISDKError constructor accepts cause as optional parameter for error chaining. Error causality chains are standard practice in error handling for debugging and production monitoring (Error Cause property - MDN Web Docs).

) {
if (GatewayAuthenticationError.isInstance(error)) {
return new AISDKError({
name: 'GatewayError',
message:
'Unauthenticated. Configure AI_GATEWAY_API_KEY or configure and use a provider module. Learn more: https://vercel.link/unauthenticated-ai-gateway-v6',
cause: error,
});
}

Expand Down
Loading