fix: catch errors from redux migration

This commit is contained in:
Paul 2021-12-25 16:37:39 +00:00
parent 028a8660c1
commit 064f223c78
3 changed files with 36 additions and 20 deletions

View file

@ -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");
}
}
}

View file

@ -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]);

View file

@ -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<LegacyState>, 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<LegacyState>, this);
await this.save();
return;
}
} catch (err) {
reportError(err, "redux_migration");
}
// Load MobX store.