Skip to content

Commit

Permalink
[BUG] customProperties parameter missing from trackException function #…
Browse files Browse the repository at this point in the history
  • Loading branch information
MSNev committed Aug 31, 2021
1 parent 3ed03e9 commit 7488905
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
93 changes: 91 additions & 2 deletions AISKU/Tests/applicationinsights.e2e.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,32 @@ export class ApplicationInsightsTests extends TestClass {
}].concat(this.asserts(1))
});

this.testCaseAsync({
name: 'E2E.GenericTests: trackException with auto telemetry sends to backend with custom properties',
stepDelay: 1,
steps: [() => {
let exception: Error = null;
try {
window['a']['b']();
Assert.ok(false, 'trackException test not run');
} catch (e) {
// Simulating window.onerror option
let autoTelemetry = {
message: e.message,
url: "https://dummy.auto.example.com",
lineNumber: 42,
columnNumber: 53,
error: e,
evt: null
} as IAutoExceptionTelemetry;

exception = e;
this._ai.trackException({ exception: autoTelemetry }, { custom: "custom value" });
}
Assert.ok(exception);
}].concat(this.asserts(1))
});

this.testCaseAsync({
name: 'E2E.GenericTests: trackException with message only sends to backend',
stepDelay: 1,
Expand Down Expand Up @@ -280,6 +306,38 @@ export class ApplicationInsightsTests extends TestClass {
}].concat(this.asserts(1))
});

this.testCaseAsync({
name: 'E2E.GenericTests: trackException with message holding error sends to backend with custom properties',
stepDelay: 1,
steps: [() => {
let exception: Error = null;
try {
window['a']['b']();
Assert.ok(false, 'trackException test not run');
} catch (e) {
// Simulating window.onerror option
let autoTelemetry = {
message: e,
url: "https://dummy.error.example.com",
lineNumber: 42,
columnNumber: 53,
error: undefined,
evt: null
} as IAutoExceptionTelemetry;

try {
exception = e;
this._ai.trackException({ exception: autoTelemetry }, { custom: "custom value" });
} catch (e) {
console.log(e);
console.log(e.stack);
Assert.ok(false, e.stack);
}
}
Assert.ok(exception);
}].concat(this.asserts(1))
});

this.testCaseAsync({
name: 'E2E.GenericTests: trackException with no Error sends to backend',
stepDelay: 1,
Expand Down Expand Up @@ -325,6 +383,37 @@ export class ApplicationInsightsTests extends TestClass {
})
});

this.testCaseAsync({
name: 'E2E.GenericTests: trackException with CustomError sends to backend with custom properties',
stepDelay: 1,
steps: [() => {
this._ai.trackException({ exception: new CustomTestError("Test Custom Error!") }, { custom: "custom value" });
}].concat(this.asserts(1)).concat(() => {
const payloadStr: string[] = this.getPayloadMessages(this.successSpy);
if (payloadStr.length > 0) {
const payload = JSON.parse(payloadStr[0]);
const data = payload.data;
Assert.ok(data, "Has Data");
if (data) {
Assert.ok(data.baseData, "Has BaseData");
let baseData = data.baseData;
if (baseData) {
const ex = baseData.exceptions[0];
Assert.ok(ex.message.indexOf("Test Custom Error!") !== -1, "Make sure the error message is present [" + ex.message + "]");
Assert.ok(ex.message.indexOf("CustomTestError") !== -1, "Make sure the error type is present [" + ex.message + "]");
Assert.equal("CustomTestError", ex.typeName, "Got the correct typename");
Assert.ok(ex.stack.length > 0, "Has stack");
Assert.ok(ex.parsedStack, "Stack was parsed");
Assert.ok(ex.hasFullStack, "Stack has been decoded");

Assert.ok(baseData.properties, "Has BaseData properties");
Assert.equal(baseData.properties.custom, "custom value");
}
}
}
})
});

this.testCaseAsync({
name: "TelemetryContext: track metric",
stepDelay: 1,
Expand Down Expand Up @@ -829,8 +918,8 @@ export class ApplicationInsightsTests extends TestClass {
});

this.testCase({
name: 'Sampling: sampleRate is generated as a field in the envelope when it is less than 100',
test: () => {
name: 'Sampling: sampleRate is generated as a field in the envelope when it is less than 100',
test: () => {
this._ai.trackEvent({ name: 'event' });
Assert.ok(this.envelopeConstructorSpy.called);
const envelope = this.envelopeConstructorSpy.returnValues[0];
Expand Down
5 changes: 3 additions & 2 deletions AISKU/src/Initialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,14 @@ export class Initialization implements IApplicationInsights {
/**
* Log an exception that you have caught.
* @param {IExceptionTelemetry} exception
* @param {{[key: string]: any}} customProperties Additional data used to filter pages and metrics in the portal. Defaults to empty.
* @memberof Initialization
*/
public trackException(exception: IExceptionTelemetry): void {
public trackException(exception: IExceptionTelemetry, customProperties?: ICustomProperties): void {
if (exception && !exception.exception && (exception as any).error) {
exception.exception = (exception as any).error;
}
this.appInsights.trackException(exception);
this.appInsights.trackException(exception, customProperties);
}

/**
Expand Down

0 comments on commit 7488905

Please sign in to comment.