From 064f223c78eabe6b59958c09be7649af4e887289 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 25 Dec 2021 16:37:39 +0000 Subject: [PATCH] fix: catch errors from redux migration --- src/context/revoltjs/SyncManager.tsx | 8 +++++++- src/lib/ErrorBoundary.tsx | 24 ++++++++++++++---------- src/mobx/State.ts | 24 +++++++++++++++--------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/context/revoltjs/SyncManager.tsx b/src/context/revoltjs/SyncManager.tsx index aa23ca6f..9b9a59c1 100644 --- a/src/context/revoltjs/SyncManager.tsx +++ b/src/context/revoltjs/SyncManager.tsx @@ -5,6 +5,8 @@ import { ClientboundNotification } from "revolt.js/dist/websocket/notifications" import { useEffect } from "preact/hooks"; +import { reportError } from "../../lib/ErrorBoundary"; + import { useApplicationState } from "../../mobx/State"; import { useClient } from "./RevoltClient"; @@ -28,7 +30,11 @@ export default function SyncManager() { if (!client) return; function onPacket(packet: ClientboundNotification) { if (packet.type === "UserSettingsUpdate") { - state.sync.apply(packet.update); + try { + state.sync.apply(packet.update); + } catch (err) { + reportError(err, "failed_sync_apply"); + } } } diff --git a/src/lib/ErrorBoundary.tsx b/src/lib/ErrorBoundary.tsx index 3f58bcba..b8359946 100644 --- a/src/lib/ErrorBoundary.tsx +++ b/src/lib/ErrorBoundary.tsx @@ -27,21 +27,25 @@ interface Props { const ERROR_URL = "https://reporting.revolt.chat"; +export function reportError(error: Error, section: string) { + stackTrace.fromError(error).then((stackframes) => + axios.post(ERROR_URL, { + stackframes, + rawStackTrace: error.stack, + origin: window.origin, + commitSHA: GIT_REVISION, + userAgent: navigator.userAgent, + section, + }), + ); +} + export default function ErrorBoundary({ children, section }: Props) { const [error, ignoreError] = useErrorBoundary(); useEffect(() => { if (error) { - stackTrace.fromError(error).then((stackframes) => - axios.post(ERROR_URL, { - stackframes, - rawStackTrace: error.stack, - origin: window.origin, - commitSHA: GIT_REVISION, - userAgent: navigator.userAgent, - section, - }), - ); + reportError(error, section); } }, [error]); diff --git a/src/mobx/State.ts b/src/mobx/State.ts index 9c5cf367..66516793 100644 --- a/src/mobx/State.ts +++ b/src/mobx/State.ts @@ -4,6 +4,8 @@ import localforage from "localforage"; import { makeAutoObservable, reaction } from "mobx"; import { Client } from "revolt.js"; +import { reportError } from "../lib/ErrorBoundary"; + import { legacyMigrateForwards, LegacyState } from "./legacy/redux"; import Persistent from "./interfaces/Persistent"; @@ -197,16 +199,20 @@ export default class State { */ async hydrate() { // Migrate legacy Redux store. - let legacy = await localforage.getItem("state"); - if (legacy) { - if (typeof legacy === "string") { - legacy = JSON.parse(legacy); - } - - legacyMigrateForwards(legacy as Partial, this); + try { + let legacy = await localforage.getItem("state"); await localforage.removeItem("state"); - await this.save(); - return; + if (legacy) { + if (typeof legacy === "string") { + legacy = JSON.parse(legacy); + } + + legacyMigrateForwards(legacy as Partial, this); + await this.save(); + return; + } + } catch (err) { + reportError(err, "redux_migration"); } // Load MobX store.