Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve properties capture for the sendErrorData method #191

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions src/common/baseTelemetrySender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class BaseTelemetrySender implements ILazyTelemetrySender {
* @param exception The exception to collect
* @param data Data associated with the exception
*/
sendErrorData(exception: Error, data?: SenderData): void {
sendErrorData(exception: Error, data?: SenderData | Record<string, any>): void {
if (!this._telemetryClient) {
if (this._instantiationStatus !== InstantiationStatus.INSTANTIATED) {
this._exceptionQueue.push({ exception, data });
Expand All @@ -74,7 +74,8 @@ export class BaseTelemetrySender implements ILazyTelemetrySender {
}
const errorData = { stack: exception.stack, message: exception.message, name: exception.name };
if (data) {
data.properties = { ...data.properties, ...errorData };
const errorProperties = data.properties || data;
data.properties = { ...errorProperties, ...errorData };
} else {
data = { properties: errorData };
}
Expand Down Expand Up @@ -126,4 +127,4 @@ export class BaseTelemetrySender implements ILazyTelemetrySender {
this._instantiationStatus = InstantiationStatus.INSTANTIATED;
});
}
}
}
43 changes: 42 additions & 1 deletion test/baseTelemetrySender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,45 @@ describe("Base telemetry sender test suite", () => {
assert.strictEqual(sender._exceptionQueue.length, 0);
assert.strictEqual((telemetryClient.logEvent as sinon.SinonSpy).callCount, 1);
});
});

describe("Send error data logic", () => {
let sender: BaseTelemetrySender;

beforeEach(async () => {
sender = new BaseTelemetrySender("key", telemetryClientFactory);
sender.instantiateSender();
// Wait 10ms to ensure that the sender has instantiated the client
await new Promise((resolve) => setTimeout(resolve, 10));
});

it("Error properties are correctly created for an empty data argument", () => {
const error = new Error("test");
sender.sendErrorData(error);
assert.strictEqual((telemetryClient.logEvent as sinon.SinonSpy).callCount, 1);
sinon.assert.calledWithMatch(
telemetryClient.logEvent as sinon.SinonSpy, "unhandlederror",
{properties: {name: error.name, message: error.message, stack: error.stack}}
);
})

it("Error properties are correctly created for a data without properties field", () => {
const error = new Error("test");
sender.sendErrorData(error, {prop1: 1, prop2: "two"});
assert.strictEqual((telemetryClient.logEvent as sinon.SinonSpy).callCount, 1);
sinon.assert.calledWithMatch(
telemetryClient.logEvent as sinon.SinonSpy, "unhandlederror",
{properties: {prop1: 1, prop2: "two", name: error.name, message: error.message, stack: error.stack}}
);
});

it("Error properties are correctly created for a data with properties field", () => {
const error = new Error("uh oh");
sender.sendErrorData(error, {properties: {prop1: 1, prop2: "two"}});
assert.strictEqual((telemetryClient.logEvent as sinon.SinonSpy).callCount, 1);
sinon.assert.calledWithMatch(
telemetryClient.logEvent as sinon.SinonSpy, "unhandlederror",
{properties: {prop1: 1, prop2: "two", name: error.name, message: error.message, stack: error.stack}}
);
});
})
});