Skip to content
Merged
Changes from all commits
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
16 changes: 11 additions & 5 deletions packages/node/src/integrations/tracing/vercelai/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getActiveSpan,
getCurrentScope,
handleCallbackErrors,
isThenable,
SDK_VERSION,
withScope,
} from '@sentry/core';
Expand Down Expand Up @@ -71,7 +72,7 @@ function isToolError(obj: unknown): obj is ToolError {
* Check for tool errors in the result and capture them
* Tool errors are not rejected in Vercel V5, it is added as metadata to the result content
*/
function checkResultForToolErrors(result: unknown | Promise<unknown>): void {
function checkResultForToolErrors(result: unknown): void {
if (typeof result !== 'object' || result === null || !('content' in result)) {
return;
}
Expand Down Expand Up @@ -236,13 +237,18 @@ export class SentryVercelAiInstrumentation extends InstrumentationBase {
};

return handleCallbackErrors(
async () => {
() => {
// @ts-expect-error we know that the method exists
const result = await originalMethod.apply(this, args);
const result = originalMethod.apply(this, args);

// Tool errors are not rejected in Vercel V5, it is added as metadata to the result content
checkResultForToolErrors(result);
if (isThenable(result)) {
// check for tool errors when the promise resolves, keep the original promise identity
result.then(checkResultForToolErrors, () => {});
Copy link
Member

Choose a reason for hiding this comment

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

hmm, I think we need to re-throw here or something like this, as otherwise this could swallow unhandled promise rejections, possibly...? Can this be? This is always hard tor reason about for me...

Copy link
Member Author

Choose a reason for hiding this comment

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

oh yah it confuses me too! I just tested this though and the error should bubble up to user as expected

return result;
}

// check for tool errors when the result is synchronous
checkResultForToolErrors(result);
return result;
},
error => {
Expand Down
Loading