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

add one more restriction on the length of exception #1990 #2006

Merged
merged 11 commits into from
Mar 6, 2023
41 changes: 40 additions & 1 deletion shared/AppInsightsCommon/Tests/Unit/src/Exception.tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Assert, AITestClass } from "@microsoft/ai-test-framework";
import { Exception } from "../../../src/applicationinsights-common"
import { DataSanitizer, Exception } from "../../../src/applicationinsights-common"
import { DiagnosticLogger } from "@microsoft/applicationinsights-core-js";
import { IExceptionInternal, IExceptionDetailsInternal, IExceptionStackFrameInternal } from "../../../src/Interfaces/IExceptionTelemetry";
import { _ExceptionDetails, _StackFrame } from "../../../src/Telemetry/Exception";
Expand Down Expand Up @@ -61,6 +61,45 @@ export class ExceptionTests extends AITestClass {
}
});

// this.testCase({
// name: "ExceptionDetails: ExceptionDetails will be truncated",
// test: () => {
// try {
// // const define
// const MAX_MESSAGE_LENGTH = DataSanitizer.MAX_MESSAGE_LENGTH;
// const messageLong = new Array(MAX_MESSAGE_LENGTH + 10).join('abc');


// let errObj = {
// reason:{
// message: "message",
// stack: messageLong + "\n" + messageLong + "\n" + messageLong
// }
// };

// let exception = Exception.CreateAutoException("message",
// "url",
// 9,
// 0,
// errObj
// );

// const exceptionDetails = new _ExceptionDetails(this.logger, exception);
// const exceptionDetailsInterface: IExceptionDetailsInternal = exceptionDetails.toInterface();

// Assert.ok(exceptionDetailsInterface.stack.length < MAX_MESSAGE_LENGTH)
// // console.log(exceptionDetailsInterface.message)
// // console.log(exceptionDetailsInterface.message.length)

// } catch (e) {
// console.log(e.stack);
// console.log(e.toString());
// Assert.ok(false, e.toString());
// }
// }
// });


this.testCase({
name: "StackFrame: StackFrame can be exported to interface format",
test: () => {
Expand Down
6 changes: 6 additions & 0 deletions shared/AppInsightsCommon/src/Telemetry/Exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,12 @@ export class _ExceptionDetails implements IExceptionDetails, ISerializable {
_self.message = dataSanitizeMessage(logger, _formatMessage(exception || error, _self.typeName)) || strNotSpecified;
const stack = exception[strStackDetails] || _getStackFromErrorObj(exception);
_self.parsedStack = _parseStack(stack);

// after parsedStack is inited, iterate over each frame object, sanitize its assembly field
if (_self.parsedStack instanceof Array){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an isArray helper function that you can use here.
But, yes this looks like it would address the raised issue.
Please go ahead and create a test case for this

arrMap(_self.parsedStack, (frame: _StackFrame) => frame.assembly = dataSanitizeString(logger, frame.assembly));
}

_self[strStack] = dataSanitizeException(logger, _formatStackTrace(stack));
_self.hasFullStack = isArray(_self.parsedStack) && _self.parsedStack.length > 0;

Expand Down