Skip to content

Commit

Permalink
[Telemetry] Bug fix in codedError.data from trackException() (#14191
Browse files Browse the repository at this point in the history
)

* Bug fix

* Simplify fix, add unit test

* Change files

* Nit: change test description
  • Loading branch information
danielayala94 authored Dec 10, 2024
1 parent 60ed57d commit bd901aa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix bug in error telemetry collection.",
"packageName": "@react-native-windows/telemetry",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ import * as errorUtils from '../utils/errorUtils';
import * as projectUtils from '../utils/projectUtils';
import * as versionUtils from '../utils/versionUtils';

class CustomTestError extends Error {
// Declare a mock errno field, so it is picked up by trackException() (see syscallExceptionFieldsToCopy)
// to copy it into codedError.data.
errno: string;

constructor(message: string) {
super(message);
this.name = 'CustomTestError';
this.errno = '123';
}
}

export class TelemetryTest extends Telemetry {
protected static hasTestTelemetryProviders: boolean;
protected static testTelemetryProvidersRan: boolean;
Expand Down Expand Up @@ -391,8 +403,16 @@ function verifyTestCommandTelemetryProcessor(
: 'Unknown',
);

// If the exception type is not CodedError but any data got copied into envelope.CodedError.data,
// for instance autolinking error info, build the expected CodedError.data.
let expectedCodedErrorData = {};
if (expectedError instanceof CustomTestError) {
expectedCodedErrorData = {errno: expectedError.errno};
}

expect(codedError.data).toStrictEqual(
(expectedError as errorUtils.CodedError).data ?? {},
(expectedError as errorUtils.CodedError).data ??
expectedCodedErrorData,
);
} else {
// If this is not error scenario, it must be a command successful event.
Expand Down Expand Up @@ -694,3 +714,29 @@ test.each(testTelemetryOptions)(
});
},
);

test.each(testTelemetryOptions)(
'A custom Error-based object with MS Build error info is copied into codedError.data appropriately by trackException()',
async options => {
await TelemetryTest.startTest(options);

const expectedError = new CustomTestError('some message');

// AI eats errors thrown in telemetry processors
const caughtErrors: Error[] = [];
TelemetryTest.addTelemetryInitializer(
verifyTestCommandTelemetryProcessor(
caughtErrors,
'Unknown',
expectedError,
),
);

await runTestCommandE2E(() => testCommandBody(expectedError));

TelemetryTest.endTest(() => {
// Check if any errors were thrown
expect(caughtErrors).toHaveLength(0);
});
},
);
2 changes: 1 addition & 1 deletion packages/@react-native-windows/telemetry/src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export class Telemetry {
const syscallExceptionFieldsToCopy = ['errno', 'syscall', 'code'];
for (const f of syscallExceptionFieldsToCopy) {
if ((error as any)[f]) {
codedErrorStruct.data.codedError.data[f] = (error as any)[f];
codedErrorStruct.data[f] = (error as any)[f];
}
}

Expand Down

0 comments on commit bd901aa

Please sign in to comment.