Skip to content

Commit 895b4d1

Browse files
committed
Catch localStorage getItem and setItem unhandled errors (#5397)
## 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 -->
1 parent e958e75 commit 895b4d1

File tree

6 files changed

+55
-14
lines changed

6 files changed

+55
-14
lines changed

.changeset/flat-emus-repeat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Catch localStorage getItem and setItem unhandled errors

apps/dashboard/src/hooks/useLocalStorage.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@ export function useLocalStorage<TType>(
1313
// FIXME: ideally we do not need localstorage like this, alernatively we move this into use-query and use-mutation to invalidate etc
1414
// eslint-disable-next-line no-restricted-syntax
1515
useEffect(() => {
16-
const item = window.localStorage.getItem(key);
17-
18-
_setValue(item ? JSON.parse(item) : initialValue);
16+
try {
17+
const item = window.localStorage.getItem(key);
18+
_setValue(item ? JSON.parse(item) : initialValue);
19+
} catch {
20+
// ignore
21+
}
1922
}, [key, initialValue]);
2023

2124
const setValue = (value_: TType) => {
2225
_setValue(value_);
2326
if (isBrowser()) {
24-
window.localStorage.setItem(key, JSON.stringify(value_));
27+
try {
28+
window.localStorage.setItem(key, JSON.stringify(value_));
29+
} catch {
30+
// ignore
31+
}
2532
}
2633
};
2734

apps/playground-web/src/app/connect/sign-in/button/RightSection.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ export function RightSection(props: {
4141
// fake login for playground
4242
const playgroundAuth: ConnectButtonProps["auth"] = {
4343
async doLogin() {
44-
localStorage.setItem("playground-loggedin", "true");
44+
try {
45+
localStorage.setItem("playground-loggedin", "true");
46+
} catch {
47+
// ignore
48+
}
4549
},
4650
async doLogout() {
4751
localStorage.removeItem("playground-loggedin");
@@ -59,7 +63,11 @@ export function RightSection(props: {
5963
};
6064
},
6165
async isLoggedIn() {
62-
return !!localStorage.getItem("playground-loggedin");
66+
try {
67+
return !!localStorage.getItem("playground-loggedin");
68+
} catch {
69+
return false;
70+
}
6371
},
6472
};
6573

apps/portal/src/components/others/Banner.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ export function Banner(props: { text: string; href: string; id: string }) {
1010
const bannerCancelledKey = `banner-cancelled${props.href}`;
1111

1212
useEffect(() => {
13-
if (localStorage.getItem(bannerCancelledKey) !== "true") {
14-
setShowBanner(true);
13+
try {
14+
if (localStorage.getItem(bannerCancelledKey) !== "true") {
15+
setShowBanner(true);
16+
}
17+
} catch {
18+
// ignore
1519
}
1620
}, [bannerCancelledKey]);
1721

@@ -41,7 +45,11 @@ export function Banner(props: { text: string; href: string; id: string }) {
4145
className="absolute right-4 shrink-0 bg-none bg-transparent p-1"
4246
onClick={() => {
4347
setShowBanner(false);
44-
localStorage.setItem(bannerCancelledKey, "true");
48+
try {
49+
localStorage.setItem(bannerCancelledKey, "true");
50+
} catch {
51+
// ignore
52+
}
4553
}}
4654
>
4755
<XIcon

apps/portal/src/components/others/theme/ThemeSwitcher.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ export function ThemeSwitcher(props: { className?: string }) {
2626
const newTheme = theme === "light" ? "dark" : "light";
2727
setTheme(newTheme);
2828
document.body.dataset.theme = newTheme;
29-
localStorage.setItem("website-theme", newTheme);
29+
try {
30+
localStorage.setItem("website-theme", newTheme);
31+
} catch {
32+
// ignore
33+
}
3034
}}
3135
variant="outline"
3236
className={cn("p-2", props.className)}

packages/thirdweb/src/utils/storage/webStorage.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ import type { AsyncStorage } from "./AsyncStorage.js";
22

33
export const webLocalStorage: AsyncStorage = {
44
async getItem(key: string) {
5-
if (typeof window !== "undefined" && window.localStorage) {
6-
return localStorage.getItem(key);
5+
try {
6+
if (typeof window !== "undefined" && window.localStorage) {
7+
return localStorage.getItem(key);
8+
}
9+
} catch {
10+
// ignore
711
}
12+
813
return null;
914
},
1015
async setItem(key: string, value: string) {
11-
if (typeof window !== "undefined" && window.localStorage) {
12-
localStorage.setItem(key, value);
16+
try {
17+
if (typeof window !== "undefined" && window.localStorage) {
18+
localStorage.setItem(key, value);
19+
}
20+
} catch {
21+
// ignore
1322
}
1423
},
1524
async removeItem(key: string) {

0 commit comments

Comments
 (0)