Skip to content

Commit b13d2c2

Browse files
authored
Merge pull request #1993 from jeddai/add-try-catch-around-local-session-storage
2 parents f83002c + 5099a5d commit b13d2c2

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased changes
22

3+
* [#1993](https://github.com/mozilla/glean.js/pull/1993): Add try-catch around uses of sessionStorage and localStorage in core.
4+
35
[Full changelog](https://github.com/mozilla/glean.js/compare/v5.0.4...main)
46

57
# v5.0.4 (2025-04-02)

glean/src/core/glean.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,20 @@ const setDebugOptionInSessionStorage = (
4747
) => {
4848
const key = `Glean.${option.toString()}`;
4949

50-
switch (option) {
51-
case DebugOption.DebugTag:
52-
sessionStorage.setItem(key, value as string);
53-
break;
54-
case DebugOption.LogPings:
55-
sessionStorage.setItem(key, (value as boolean).toString());
56-
break;
57-
case DebugOption.SourceTags:
58-
sessionStorage.setItem(key, (value as string[]).join(","));
59-
break;
50+
try {
51+
switch (option) {
52+
case DebugOption.DebugTag:
53+
sessionStorage.setItem(key, value as string);
54+
break;
55+
case DebugOption.LogPings:
56+
sessionStorage.setItem(key, (value as boolean).toString());
57+
break;
58+
case DebugOption.SourceTags:
59+
sessionStorage.setItem(key, (value as string[]).join(","));
60+
break;
61+
}
62+
} catch (e) {
63+
console.warn(e);
6064
}
6165
};
6266

@@ -69,7 +73,12 @@ const setDebugOptionInSessionStorage = (
6973
const getDebugOptionFromSessionStorage = (
7074
option: DebugOptionValue
7175
): string | undefined => {
72-
return sessionStorage.getItem(`Glean.${option.toString()}`) || undefined;
76+
try {
77+
return sessionStorage.getItem(`Glean.${option.toString()}`) || undefined;
78+
} catch (e) {
79+
console.warn(e);
80+
return undefined;
81+
}
7382
};
7483

7584
namespace Glean {

glean/src/core/internal_metrics.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,12 @@ export class CoreMetrics {
209209
this.generateNewSession();
210210
}
211211

212-
// Update the last-active timestamp in LocalStorage to the current time.
213-
localStorage.setItem("glean_session_last_active", Date.now().toString());
212+
try {
213+
// Update the last-active timestamp in LocalStorage to the current time.
214+
localStorage.setItem("glean_session_last_active", Date.now().toString());
215+
} catch (e) {
216+
console.warn(e);
217+
}
214218
}
215219

216220
/**

glean/src/core/sessions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
* @returns {boolean} If the current session is inactive.
1010
*/
1111
export function isSessionInactive(sessionLengthInMinutes = 30): boolean {
12-
const lastActive = localStorage.getItem("glean_session_last_active");
12+
let lastActive: string | null = null;
13+
try {
14+
lastActive = localStorage.getItem("glean_session_last_active");
15+
} catch (e) {
16+
console.warn(e);
17+
}
1318
const lastActiveDate = new Date(Number(lastActive));
1419

1520
// Subtract the session length from the current date.

0 commit comments

Comments
 (0)