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

fix: Use validated exception data in dispatchError. #562

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import parse from './stack/StackParser';
const CUSTOM_KEY_PREFIX = '$ld:telemetry';
const ERROR_KEY = `${CUSTOM_KEY_PREFIX}:error`;
const SESSION_CAPTURE_KEY = `${CUSTOM_KEY_PREFIX}:sessionCapture`;
const GENERIC_EXCEPTION = 'generic';
const NULL_EXCEPTION_MESSAGE = 'exception was null or undefined';
const MISSING_MESSAGE = 'exception had no message';

function safeValue(u: unknown): string | boolean | number | undefined {
switch (typeof u) {
Expand Down Expand Up @@ -138,21 +141,22 @@ export default class BrowserTelemetryImpl implements BrowserTelemetry {

const data: ErrorData = validException
? {
type: exception.name || exception.constructor?.name || 'generic',
message: exception.message,
type: exception.name || exception.constructor?.name || GENERIC_EXCEPTION,
// Only coalesce null/undefined, not empty.
message: exception.message ?? MISSING_MESSAGE,
Copy link
Member Author

Choose a reason for hiding this comment

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

In practice this shouldn't happen, but may was well be ultra safe.

stack: parse(exception, this.options.stack),
breadcrumbs: [...this.breadcrumbs],
sessionId: this.sessionId,
}
: {
type: 'generic',
message: 'null or undefined exception',
type: GENERIC_EXCEPTION,
message: NULL_EXCEPTION_MESSAGE,
stack: { frames: [] },
breadcrumbs: [...this.breadcrumbs],
sessionId: this.sessionId,
};
this.capture(ERROR_KEY, data);
this.dispatchError(exception);
this.dispatchError(data);
Copy link
Member Author

Choose a reason for hiding this comment

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

This will have the already checked type and message.

}

captureErrorEvent(errorEvent: ErrorEvent): void {
Expand Down Expand Up @@ -207,9 +211,9 @@ export default class BrowserTelemetryImpl implements BrowserTelemetry {
this.dispatchFlagDetailChanged(flagKey, detail);
}

private dispatchError(exception: Error) {
private dispatchError(exception: ErrorData) {
this.collectors.forEach((collector) => {
collector.handleErrorEvent?.(exception.name, exception.message);
collector.handleErrorEvent?.(exception.type, exception.message);
});
}

Expand Down