fix: add error handling for JSON.parse in _restorePowers()#2975
fix: add error handling for JSON.parse in _restorePowers()#2975lawrence3699 wants to merge 1 commit intoFreezingMoon:masterfrom
Conversation
Cookies.get() can return undefined, and the cookie value may contain invalid JSON (browser storage corruption, manual editing). The current code calls JSON.parse() without try-catch, which throws an unhandled exception and crashes the meta powers UI. Added null checks and try-catch with console error logging.
|
@lawrence3699 is attempting to deploy a commit to the FreezingMoon Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR hardens the Meta Powers UI cookie restore path to avoid crashes when the persisted cookie is missing, malformed, or contains unexpected data.
Changes:
- Added null checks around
Cookies.get()and parsed JSON content before accessingtoggles. - Wrapped cookie parsing/restoration in a
try/catchand added error logging. - Made restoration more tolerant via optional chaining on
powers[key]?.enabled.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (powers[key]?.enabled) { | ||
| this._togglePower(key, this[`btn${capitalize(key)}`]); | ||
| } |
There was a problem hiding this comment.
key comes from cookie data and may not correspond to a valid meta power. If key is unknown, _togglePower(key, ...) will throw when it accesses this.toggles[stateKey], and this[btn${capitalize(key)}] may be undefined. Consider restricting restoration to known keys (e.g., only keys present in this.toggles) and verifying the button instance exists before calling _togglePower, so a partially corrupted cookie can still restore valid toggles without hitting the catch-all error path.
| if (powers[key]?.enabled) { | |
| this._togglePower(key, this[`btn${capitalize(key)}`]); | |
| } | |
| // Only restore powers that are known and have an associated button | |
| if (!Object.prototype.hasOwnProperty.call(this.toggles, key)) { | |
| return; | |
| } | |
| const button = this[`btn${capitalize(key)}` as keyof this] as unknown as Button | undefined; | |
| if (!button) { | |
| return; | |
| } | |
| if (powers[key]?.enabled && !this.toggles[key].enabled) { | |
| this._togglePower(key, button); | |
| } |
| } catch (error) { | ||
| console.error('Failed to restore meta powers from cookie:', error); | ||
| } |
There was a problem hiding this comment.
In the catch, the code logs the parse failure but leaves the corrupted cookie intact. This will trigger the same error on every page load until the user clears cookies manually. Consider removing/resetting COOKIE_KEY when restoration fails (or when the parsed shape is invalid) to allow a clean fallback on subsequent loads.
Summary
JSON.parse(Cookies.get(COOKIE_KEY))in try-catch with null checksCookies.get()can returnundefined, causingJSON.parse(undefined)to throwSyntaxErrorChanges
parsed.togglesbefore iteratingpowers[key]?.enabledTest plan