Skip to content

Commit 6dd34ba

Browse files
committed
truncate message and add tests
1 parent c75778e commit 6dd34ba

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +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';
39+
import { truncateString } from './util';
4040

4141
let _trackers: Record<string, BrowserTracker> = {};
4242

@@ -75,7 +75,8 @@ export function trackError(
7575
trackers: Array<string> = Object.keys(_trackers)
7676
) {
7777
const { message, filename, lineno, colno, error, context, timestamp } = event,
78-
stack = error && truncateStackTrace(error.stack);
78+
stack = error && truncateString(error.stack, 8192),
79+
truncatedMessage = message && truncateString(message, 2048);
7980

8081
dispatchToTrackersInCollection(trackers, _trackers, (t) => {
8182
t.core.track(
@@ -84,7 +85,7 @@ export function trackError(
8485
schema: 'iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1',
8586
data: {
8687
programmingLanguage: 'JAVASCRIPT',
87-
message: message ?? "JS Exception. Browser doesn't support ErrorEvent API",
88+
message: truncatedMessage ?? "JS Exception. Browser doesn't support ErrorEvent API",
8889
stackTrace: stack,
8990
lineNumber: lineno,
9091
lineColumn: colno,
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
/**
2-
* Truncate stack trace if it's size is greater than 8kb
2+
* Truncate string if it's longer than 8192 chars
33
*
4-
* @param stackTrace - The stack trace to truncate
5-
* @returns The trucated stack trace
4+
* @param string - The stack trace to truncate
5+
* @param maxLength - The maximum length of the truncated string. Default = 8192
6+
* @returns The truncated string
67
*/
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;
8+
export function truncateString(string?: string, maxLength: number = 8192): string | undefined {
9+
if (string && string.length > 8192) {
10+
const truncatedString = string.substring(0, maxLength);
11+
return truncatedString;
1812
}
1913

20-
return stackTrace;
14+
return string;
2115
}

plugins/browser-plugin-error-tracking/test/events.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,18 @@ describe('AdTrackingPlugin', () => {
6060
encodeBase64: false,
6161
plugins: [ErrorTrackingPlugin()],
6262
});
63+
addTracker('sp4', 'sp4', 'js-3.0.0', '', state, {
64+
encodeBase64: false,
65+
plugins: [ErrorTrackingPlugin()],
66+
});
6367

6468
const error = new Error('this is an error');
6569
error.stack = 'stacktrace-1';
6670

71+
const oversizedError = new Error('this is a longer error');
72+
oversizedError.stack = 'x'.repeat(10000);
73+
const oversizedMessage = 'y'.repeat(10000);
74+
6775
trackError(
6876
{
6977
message: 'message-1',
@@ -89,6 +97,14 @@ describe('AdTrackingPlugin', () => {
8997
['sp3']
9098
);
9199

100+
trackError(
101+
{
102+
message: oversizedMessage,
103+
error: oversizedError,
104+
},
105+
['sp4']
106+
);
107+
92108
it('trackError adds the expected application error event to the queue', () => {
93109
expect(
94110
extractUeEvent('iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1').from(state.outQueues[0])
@@ -126,4 +142,16 @@ describe('AdTrackingPlugin', () => {
126142
},
127143
});
128144
});
145+
146+
it('trackError replaces undefined messages with placeholder', () => {
147+
expect(
148+
extractUeEvent('iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1').from(state.outQueues[3])
149+
).toMatchObject({
150+
schema: 'iglu:com.snowplowanalytics.snowplow/application_error/jsonschema/1-0-1',
151+
data: {
152+
message: 'y'.repeat(2048),
153+
stackTrace: 'x'.repeat(8192),
154+
},
155+
});
156+
});
129157
});

0 commit comments

Comments
 (0)