Skip to content

Commit 9204ce9

Browse files
authored
ENG-942 Canvas migration bug (#493)
* rm initialData, add loadSnapshot * Enhance error handling in useRoamStore by introducing a dedicated error handler for store creation and snapshot migration failures. This improves error logging and email notifications for better debugging and user support.
1 parent 2d20588 commit 9204ce9

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

apps/roam/src/components/canvas/useRoamStore.ts

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,32 +131,64 @@ export const useRoamStore = ({
131131
setLoading(false);
132132
}
133133
}, [tree, pageUid]);
134+
134135
const store = useMemo(() => {
135136
if (needsUpgrade || error || loading) return null;
137+
138+
const handleStoreError = ({
139+
error,
140+
errorMessage,
141+
}: {
142+
error: Error;
143+
errorMessage: string;
144+
}): void => {
145+
console.error(errorMessage, error);
146+
setError(error);
147+
setLoading(false);
148+
const snapshotSize = initialSnapshot
149+
? JSON.stringify(initialSnapshot).length
150+
: 0;
151+
sendErrorEmail({
152+
error,
153+
type: errorMessage,
154+
context: {
155+
pageUid,
156+
user: getCurrentUserDisplayName(),
157+
snapshotSize,
158+
...(snapshotSize < 10000 ? { initialSnapshot } : {}),
159+
},
160+
}).catch(() => {});
161+
};
162+
136163
// eslint-disable-next-line @typescript-eslint/naming-convention
137164
let _store: TLStore;
165+
138166
try {
139167
_store = createTLStore({
140-
initialData: initialSnapshot?.store,
141168
migrations: migrations,
142169
shapeUtils: [...defaultShapeUtils, ...customShapeUtils],
143170
bindingUtils: [...defaultBindingUtils, ...customBindingUtils],
144171
});
145172
} catch (e) {
146-
const error = e as Error;
147-
console.error("Failed to create store:", error);
148-
sendErrorEmail({
149-
error,
150-
type: "Failed to create TLStore",
151-
context: {
152-
pageUid,
153-
user: getCurrentUserDisplayName(),
154-
initialSnapshot,
155-
},
156-
}).catch(() => {});
173+
handleStoreError({
174+
error: e as Error,
175+
errorMessage: "Failed to create TLStore",
176+
});
157177
return null;
158178
}
159179

180+
if (initialSnapshot) {
181+
try {
182+
loadSnapshot(_store, initialSnapshot);
183+
} catch (e) {
184+
handleStoreError({
185+
error: e as Error,
186+
errorMessage: "Failed to migrate snapshot",
187+
});
188+
return null;
189+
}
190+
}
191+
160192
_store.listen((rec) => {
161193
if (rec.source !== "user") return;
162194
const validChanges = Object.keys(rec.changes.added)

0 commit comments

Comments
 (0)