Skip to content

Commit 60e0864

Browse files
authored
Merge pull request #191 from ilia-db/send-error-properties
Improve properties capture for the sendErrorData method
2 parents cd3176a + 493e811 commit 60e0864

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/common/baseTelemetrySender.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class BaseTelemetrySender implements ILazyTelemetrySender {
6565
* @param exception The exception to collect
6666
* @param data Data associated with the exception
6767
*/
68-
sendErrorData(exception: Error, data?: SenderData): void {
68+
sendErrorData(exception: Error, data?: SenderData | Record<string, any>): void {
6969
if (!this._telemetryClient) {
7070
if (this._instantiationStatus !== InstantiationStatus.INSTANTIATED) {
7171
this._exceptionQueue.push({ exception, data });
@@ -74,7 +74,8 @@ export class BaseTelemetrySender implements ILazyTelemetrySender {
7474
}
7575
const errorData = { stack: exception.stack, message: exception.message, name: exception.name };
7676
if (data) {
77-
data.properties = { ...data.properties, ...errorData };
77+
const errorProperties = data.properties || data;
78+
data.properties = { ...errorProperties, ...errorData };
7879
} else {
7980
data = { properties: errorData };
8081
}
@@ -126,4 +127,4 @@ export class BaseTelemetrySender implements ILazyTelemetrySender {
126127
this._instantiationStatus = InstantiationStatus.INSTANTIATED;
127128
});
128129
}
129-
}
130+
}

test/baseTelemetrySender.test.ts

+42-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,45 @@ describe("Base telemetry sender test suite", () => {
5858
assert.strictEqual(sender._exceptionQueue.length, 0);
5959
assert.strictEqual((telemetryClient.logEvent as sinon.SinonSpy).callCount, 1);
6060
});
61-
});
61+
62+
describe("Send error data logic", () => {
63+
let sender: BaseTelemetrySender;
64+
65+
beforeEach(async () => {
66+
sender = new BaseTelemetrySender("key", telemetryClientFactory);
67+
sender.instantiateSender();
68+
// Wait 10ms to ensure that the sender has instantiated the client
69+
await new Promise((resolve) => setTimeout(resolve, 10));
70+
});
71+
72+
it("Error properties are correctly created for an empty data argument", () => {
73+
const error = new Error("test");
74+
sender.sendErrorData(error);
75+
assert.strictEqual((telemetryClient.logEvent as sinon.SinonSpy).callCount, 1);
76+
sinon.assert.calledWithMatch(
77+
telemetryClient.logEvent as sinon.SinonSpy, "unhandlederror",
78+
{properties: {name: error.name, message: error.message, stack: error.stack}}
79+
);
80+
})
81+
82+
it("Error properties are correctly created for a data without properties field", () => {
83+
const error = new Error("test");
84+
sender.sendErrorData(error, {prop1: 1, prop2: "two"});
85+
assert.strictEqual((telemetryClient.logEvent as sinon.SinonSpy).callCount, 1);
86+
sinon.assert.calledWithMatch(
87+
telemetryClient.logEvent as sinon.SinonSpy, "unhandlederror",
88+
{properties: {prop1: 1, prop2: "two", name: error.name, message: error.message, stack: error.stack}}
89+
);
90+
});
91+
92+
it("Error properties are correctly created for a data with properties field", () => {
93+
const error = new Error("uh oh");
94+
sender.sendErrorData(error, {properties: {prop1: 1, prop2: "two"}});
95+
assert.strictEqual((telemetryClient.logEvent as sinon.SinonSpy).callCount, 1);
96+
sinon.assert.calledWithMatch(
97+
telemetryClient.logEvent as sinon.SinonSpy, "unhandlederror",
98+
{properties: {prop1: 1, prop2: "two", name: error.name, message: error.message, stack: error.stack}}
99+
);
100+
});
101+
})
102+
});

0 commit comments

Comments
 (0)