Skip to content

Commit c75778e

Browse files
committed
add trucate function to application error event
1 parent 36e1d63 commit c75778e

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-plugin-error-tracking",
5+
"comment": "Truncate error stack trace if it's size is greater than 8kb",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-plugin-error-tracking"
10+
}

plugins/browser-plugin-error-tracking/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
dispatchToTrackersInCollection,
3737
} from '@snowplow/browser-tracker-core';
3838
import { buildSelfDescribingEvent, CommonEventProperties, SelfDescribingJson } from '@snowplow/tracker-core';
39+
import { truncateStackTrace } from './util';
3940

4041
let _trackers: Record<string, BrowserTracker> = {};
4142

@@ -74,7 +75,7 @@ export function trackError(
7475
trackers: Array<string> = Object.keys(_trackers)
7576
) {
7677
const { message, filename, lineno, colno, error, context, timestamp } = event,
77-
stack = error && error.stack ? error.stack : null;
78+
stack = error && truncateStackTrace(error.stack);
7879

7980
dispatchToTrackersInCollection(trackers, _trackers, (t) => {
8081
t.core.track(
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Truncate stack trace if it's size is greater than 8kb
3+
*
4+
* @param stackTrace - The stack trace to truncate
5+
* @returns The trucated stack trace
6+
*/
7+
export function truncateStackTrace(stackTrace?: string): string | undefined {
8+
const byteSize = (str: string) => new Blob([str]).size;
9+
10+
if (stackTrace && byteSize(stackTrace) > 8000) {
11+
const encoder = new TextEncoder();
12+
const encodedText = encoder.encode(stackTrace);
13+
const truncatedEncodedText = encodedText.slice(0, 8000);
14+
const decoder = new TextDecoder();
15+
const truncatedStackTrace = decoder.decode(truncatedEncodedText);
16+
17+
return truncatedStackTrace;
18+
}
19+
20+
return stackTrace;
21+
}

0 commit comments

Comments
 (0)