Skip to content

Commit a69d4a2

Browse files
committed
fix(node): Handle unhandled rejection for Vercel AI errors
1 parent 0e0c711 commit a69d4a2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

packages/node/src/integrations/tracing/vercelai/instrumentation.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,48 @@ export class SentryVercelAiInstrumentation extends InstrumentationBase {
202202
}
203203
}
204204

205+
/**
206+
* Sets up global error handling for Vercel AI stream processing errors
207+
*/
208+
private _setupGlobalErrorHandling(): void {
209+
// Add a global unhandled rejection handler specifically for Vercel AI errors
210+
const originalHandler = process.listeners('unhandledRejection');
211+
const aiErrorHandler = (reason: unknown, promise: Promise<unknown>): void => {
212+
// Check if this is a Vercel AI error
213+
if (reason && typeof reason === 'object' && reason !== null && Symbol.for('vercel.ai.error') in reason) {
214+
// Add Sentry context to the error
215+
if (reason && typeof reason === 'object') {
216+
addNonEnumerableProperty(reason, '_sentry_active_span', getActiveSpan());
217+
}
218+
219+
// Don't re-throw the error to prevent it from becoming unhandled
220+
return;
221+
}
222+
223+
// For non-AI errors, let the original handler deal with it
224+
if (originalHandler.length > 0) {
225+
originalHandler.forEach(handler => {
226+
if (typeof handler === 'function') {
227+
handler(reason, promise);
228+
}
229+
});
230+
}
231+
};
232+
233+
// Remove any existing unhandled rejection handlers and add our AI-specific one
234+
process.removeAllListeners('unhandledRejection');
235+
process.on('unhandledRejection', aiErrorHandler);
236+
}
237+
205238
/**
206239
* Patches module exports to enable Vercel AI telemetry.
207240
*/
208241
private _patch(moduleExports: PatchedModuleExports): unknown {
209242
this._isPatched = true;
210243

244+
// Set up global error handling for Vercel AI stream processing errors
245+
this._setupGlobalErrorHandling();
246+
211247
this._callbacks.forEach(callback => callback());
212248
this._callbacks = [];
213249

0 commit comments

Comments
 (0)