Skip to content

fix(gateway): remove cause from custom handling of GatewayAuthenticationError error#11111

Closed
gr2m wants to merge 3 commits intomainfrom
remove-cause-from-gateway-unauthenticated-error
Closed

fix(gateway): remove cause from custom handling of GatewayAuthenticationError error#11111
gr2m wants to merge 3 commits intomainfrom
remove-cause-from-gateway-unauthenticated-error

Conversation

@gr2m
Copy link
Copy Markdown
Collaborator

@gr2m gr2m commented Dec 11, 2025

No description provided.

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?

@gr2m gr2m added the backport Admins only: add this label to a pull request in order to backport it to the prior version label Dec 11, 2025
export function wrapGatewayError(error: unknown): unknown {
if (
GatewayAuthenticationError.isInstance(error) ||
GatewayModelNotFoundError.isInstance(error)
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).

@gr2m gr2m changed the title gateway: remove cause from custom handling of GatewayAuthenticationError error fix(gateway): remove cause from custom handling of GatewayAuthenticationError error Dec 11, 2025
@gr2m
Copy link
Copy Markdown
Collaborator Author

gr2m commented Dec 11, 2025

superseded by #11113

@gr2m gr2m closed this Dec 11, 2025
@gr2m gr2m added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/gateway Issues related to the @ai-sdk/gateway provider and removed ai/gateway labels Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label backport Admins only: add this label to a pull request in order to backport it to the prior version provider/gateway Issues related to the @ai-sdk/gateway provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant