11/* eslint-disable @typescript-eslint/no-explicit-any */
22
3- // npx vitest run src/__tests__/PostHogTelemetryClient.test.ts
3+ // pnpm --filter @roo-code/telemetry test src/__tests__/PostHogTelemetryClient.test.ts
44
55import * as vscode from "vscode"
66import { PostHog } from "posthog-node"
@@ -452,7 +452,7 @@ describe("PostHogTelemetryClient", () => {
452452 expect ( mockPostHogClient . captureException ) . toHaveBeenCalledWith ( error , "test-machine-id" , undefined )
453453 } )
454454
455- it ( "should use nested error message from OpenAI SDK error structure" , ( ) => {
455+ it ( "should use nested error message from OpenAI SDK error structure for filtering " , ( ) => {
456456 const client = new PostHogTelemetryClient ( )
457457 client . updateTelemetryState ( true )
458458
@@ -470,6 +470,61 @@ describe("PostHogTelemetryClient", () => {
470470 expect ( mockPostHogClient . captureException ) . not . toHaveBeenCalled ( )
471471 } )
472472
473+ it ( "should modify error.message with extracted message from nested metadata.raw" , ( ) => {
474+ const client = new PostHogTelemetryClient ( )
475+ client . updateTelemetryState ( true )
476+
477+ // Create an OpenAI SDK-like error with nested metadata (non-rate-limit error)
478+ const error = Object . assign ( new Error ( "Generic request failed" ) , {
479+ status : 500 ,
480+ error : {
481+ message : "Nested error message" ,
482+ metadata : { raw : "Upstream provider error: model overloaded" } ,
483+ } ,
484+ } )
485+
486+ client . captureException ( error )
487+
488+ // Verify error message was modified to use metadata.raw (highest priority)
489+ expect ( error . message ) . toBe ( "Upstream provider error: model overloaded" )
490+ expect ( mockPostHogClient . captureException ) . toHaveBeenCalledWith ( error , "test-machine-id" , undefined )
491+ } )
492+
493+ it ( "should modify error.message with nested error.message when metadata.raw is not available" , ( ) => {
494+ const client = new PostHogTelemetryClient ( )
495+ client . updateTelemetryState ( true )
496+
497+ // Create an OpenAI SDK-like error with nested message but no metadata.raw
498+ const error = Object . assign ( new Error ( "Generic request failed" ) , {
499+ status : 500 ,
500+ error : {
501+ message : "Upstream provider: connection timeout" ,
502+ } ,
503+ } )
504+
505+ client . captureException ( error )
506+
507+ // Verify error message was modified to use nested error.message
508+ expect ( error . message ) . toBe ( "Upstream provider: connection timeout" )
509+ expect ( mockPostHogClient . captureException ) . toHaveBeenCalledWith ( error , "test-machine-id" , undefined )
510+ } )
511+
512+ it ( "should use primary message when no nested error structure exists" , ( ) => {
513+ const client = new PostHogTelemetryClient ( )
514+ client . updateTelemetryState ( true )
515+
516+ // Create an OpenAI SDK-like error without nested error object
517+ const error = Object . assign ( new Error ( "Primary error message" ) , {
518+ status : 500 ,
519+ } )
520+
521+ client . captureException ( error )
522+
523+ // Verify error message remains the primary message
524+ expect ( error . message ) . toBe ( "Primary error message" )
525+ expect ( mockPostHogClient . captureException ) . toHaveBeenCalledWith ( error , "test-machine-id" , undefined )
526+ } )
527+
473528 it ( "should auto-extract properties from ApiProviderError" , ( ) => {
474529 const client = new PostHogTelemetryClient ( )
475530 client . updateTelemetryState ( true )
0 commit comments