Skip to content

Commit 971031b

Browse files
author
Vanshika
committed
Add error handling to localStorage operations
Wrap localStorage.setItem() calls in try-catch blocks to prevent crashes when storage is unavailable (e.g., private browsing mode, quota exceeded, or security restrictions). Modified files: - js/themebox.js: Theme preference storage - js/languagebox.js: Language preference storage - js/widgets/reflection.js: Analysis report storage All localStorage operations now gracefully handle errors with console.warn logging, ensuring the application remains functional even when storage fails.
1 parent fe8d4f6 commit 971031b

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

js/languagebox.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,11 @@ class LanguageBox {
234234
this._language = this._language.split("-")[0];
235235
}
236236

237-
localStorage.setItem("languagePreference", this._language);
237+
try {
238+
localStorage.setItem("languagePreference", this._language);
239+
} catch (e) {
240+
console.warn("Could not save language preference:", e);
241+
}
238242
this.activity.textMsg(_("Music Blocks is already set to this language."));
239243
} else {
240244
this.activity.storage.languagePreference = this._language;

js/themebox.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ class ThemeBox {
263263
} else {
264264
// Save preference to localStorage
265265
this.activity.storage.themePreference = this._theme;
266-
localStorage.setItem("themePreference", this._theme);
266+
try {
267+
localStorage.setItem("themePreference", this._theme);
268+
} catch (e) {
269+
console.warn("Could not save theme preference:", e);
270+
}
267271

268272
// Apply theme instantly instead of reloading
269273
this.applyThemeInstantly();

js/widgets/reflection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,11 @@ class ReflectionMatrix {
575575
*/
576576
saveReport(data) {
577577
const key = "musicblocks_analysis";
578-
localStorage.setItem(key, data.response);
578+
try {
579+
localStorage.setItem(key, data.response);
580+
} catch (e) {
581+
console.warn("Could not save analysis report to localStorage:", e);
582+
}
579583
console.log("Conversation saved in localStorage.");
580584
}
581585

0 commit comments

Comments
 (0)