Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

Wrap localStorage API Access in try-catch (#1241)

Shriram Balaji22160f50d355
diff --git a/src/components/TopErrorBoundary.tsx b/src/components/TopErrorBoundary.tsx
index a07410366a..bd386b4b0c 100644
--- a/src/components/TopErrorBoundary.tsx
+++ b/src/components/TopErrorBoundary.tsx
@@ -81,8 +81,12 @@ export class TopErrorBoundary extends React.Component<
{t("errorSplash.clearCanvasMessage")}
<button
onClick={() => {
- localStorage.clear();
- window.location.reload();
+ try {
+ localStorage.clear();
+ window.location.reload();
+ } catch (error) {
+ console.error(error);
+ }
}}
>
{t("errorSplash.clearCanvasMessage_button")}
diff --git a/src/data/localStorage.ts b/src/data/localStorage.ts
index 25ea246ee4..2c283a9980 100644
--- a/src/data/localStorage.ts
+++ b/src/data/localStorage.ts
@@ -10,19 +10,32 @@ export function saveToLocalStorage(
elements: readonly ExcalidrawElement[],
appState: AppState,
) {
- localStorage.setItem(
- LOCAL_STORAGE_KEY,
- JSON.stringify(elements.filter((element) => !element.isDeleted)),
- );
- localStorage.setItem(
- LOCAL_STORAGE_KEY_STATE,
- JSON.stringify(clearAppStateForLocalStorage(appState)),
- );
+ try {
+ localStorage.setItem(
+ LOCAL_STORAGE_KEY,
+ JSON.stringify(elements.filter((element) => !element.isDeleted)),
+ );
+ localStorage.setItem(
+ LOCAL_STORAGE_KEY_STATE,
+ JSON.stringify(clearAppStateForLocalStorage(appState)),
+ );
+ } catch (error) {
+ // Unable to access window.localStorage
+ console.error(error);
+ }
}
export function restoreFromLocalStorage() {
- const savedElements = localStorage.getItem(LOCAL_STORAGE_KEY);
- const savedState = localStorage.getItem(LOCAL_STORAGE_KEY_STATE);
+ let savedElements = null;
+ let savedState = null;
+
+ try {
+ savedElements = localStorage.getItem(LOCAL_STORAGE_KEY);
+ savedState = localStorage.getItem(LOCAL_STORAGE_KEY_STATE);
+ } catch (error) {
+ // Unable to access localStorage
+ console.error(error);
+ }
let elements = [];
if (savedElements) {