Add error handling to localStorage operations#5329
Add error handling to localStorage operations#5329walterbender merged 1 commit intosugarlabs:masterfrom
Conversation
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.
|
✅ All Jest tests passed! This PR is ready to merge. |
|
Hi @walterbender @vyagh @chimosky I’ve tested the changes locally (including Safari Private Mode), and the UI now degrades gracefully while logging warnings instead of throwing uncaught exceptions. The implementation follows the existing pattern already used in js/activity.js. Whenever you have time, I’d appreciate a review. Happy to make any adjustments or improvements if needed — thanks for your time and guidance! |
|
Looks good but please remove the unrelated PR for abc test. |
776e9bd to
971031b
Compare
|
❌ Some Jest tests failed. Please check the logs and fix the issues before merging. Failed Tests: |
Summary
This PR adds
try-catchblocks tolocalStorage.setItem()operations across several components to prevent the application from crashing when browser storage is restricted (e.g., in Private Browsing mode or when the storage quota is exceeded).Fixes #5326
Problem
In many browsers (specifically Safari and Firefox in Private Mode),
localStorageread/write operations can throw security exceptions. Additionally, if the disk space is full, it throws aQuotaExceededError. Previously, these were unhandled, causing parts of the UI (theme switching, language switching) to fail silently or crash the script execution.Solution
Wrapped
localStorage.setItemcalls in the following files with defensivetry-catchblocks:This ensures that even if persistence fails, the application logic continues to run, providing a better user experience through graceful degradation.
Visual Proof
Before Fix
After Fix
Checklist