Skip to content

Commit

Permalink
Catch localStorage getItem and setItem unhandled errors (#5397)
Browse files Browse the repository at this point in the history
## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on enhancing error handling for `localStorage` operations across various components, ensuring that unhandled errors do not disrupt functionality.

### Detailed summary
- Wrapped `localStorage.setItem` and `localStorage.getItem` calls in `try-catch` blocks to handle potential errors.
- Updated `ThemeSwitcher`, `RightSection`, `useLocalStorage`, `Banner`, and `webLocalStorage` to ignore errors gracefully.
- Maintained existing functionality while improving robustness.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
  • Loading branch information
MananTank committed Nov 14, 2024
1 parent e958e75 commit 895b4d1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-emus-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Catch localStorage getItem and setItem unhandled errors
15 changes: 11 additions & 4 deletions apps/dashboard/src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ export function useLocalStorage<TType>(
// FIXME: ideally we do not need localstorage like this, alernatively we move this into use-query and use-mutation to invalidate etc
// eslint-disable-next-line no-restricted-syntax
useEffect(() => {
const item = window.localStorage.getItem(key);

_setValue(item ? JSON.parse(item) : initialValue);
try {
const item = window.localStorage.getItem(key);
_setValue(item ? JSON.parse(item) : initialValue);
} catch {
// ignore
}
}, [key, initialValue]);

const setValue = (value_: TType) => {
_setValue(value_);
if (isBrowser()) {
window.localStorage.setItem(key, JSON.stringify(value_));
try {
window.localStorage.setItem(key, JSON.stringify(value_));
} catch {
// ignore
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export function RightSection(props: {
// fake login for playground
const playgroundAuth: ConnectButtonProps["auth"] = {
async doLogin() {
localStorage.setItem("playground-loggedin", "true");
try {
localStorage.setItem("playground-loggedin", "true");
} catch {
// ignore
}
},
async doLogout() {
localStorage.removeItem("playground-loggedin");
Expand All @@ -59,7 +63,11 @@ export function RightSection(props: {
};
},
async isLoggedIn() {
return !!localStorage.getItem("playground-loggedin");
try {
return !!localStorage.getItem("playground-loggedin");
} catch {
return false;
}
},
};

Expand Down
14 changes: 11 additions & 3 deletions apps/portal/src/components/others/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ export function Banner(props: { text: string; href: string; id: string }) {
const bannerCancelledKey = `banner-cancelled${props.href}`;

useEffect(() => {
if (localStorage.getItem(bannerCancelledKey) !== "true") {
setShowBanner(true);
try {
if (localStorage.getItem(bannerCancelledKey) !== "true") {
setShowBanner(true);
}
} catch {
// ignore
}
}, [bannerCancelledKey]);

Expand Down Expand Up @@ -41,7 +45,11 @@ export function Banner(props: { text: string; href: string; id: string }) {
className="absolute right-4 shrink-0 bg-none bg-transparent p-1"
onClick={() => {
setShowBanner(false);
localStorage.setItem(bannerCancelledKey, "true");
try {
localStorage.setItem(bannerCancelledKey, "true");
} catch {
// ignore
}
}}
>
<XIcon
Expand Down
6 changes: 5 additions & 1 deletion apps/portal/src/components/others/theme/ThemeSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export function ThemeSwitcher(props: { className?: string }) {
const newTheme = theme === "light" ? "dark" : "light";
setTheme(newTheme);
document.body.dataset.theme = newTheme;
localStorage.setItem("website-theme", newTheme);
try {
localStorage.setItem("website-theme", newTheme);
} catch {
// ignore
}
}}
variant="outline"
className={cn("p-2", props.className)}
Expand Down
17 changes: 13 additions & 4 deletions packages/thirdweb/src/utils/storage/webStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import type { AsyncStorage } from "./AsyncStorage.js";

export const webLocalStorage: AsyncStorage = {
async getItem(key: string) {
if (typeof window !== "undefined" && window.localStorage) {
return localStorage.getItem(key);
try {
if (typeof window !== "undefined" && window.localStorage) {
return localStorage.getItem(key);
}
} catch {
// ignore
}

return null;
},
async setItem(key: string, value: string) {
if (typeof window !== "undefined" && window.localStorage) {
localStorage.setItem(key, value);
try {
if (typeof window !== "undefined" && window.localStorage) {
localStorage.setItem(key, value);
}
} catch {
// ignore
}
},
async removeItem(key: string) {
Expand Down

0 comments on commit 895b4d1

Please sign in to comment.