Skip to content

Commit 6e47ec6

Browse files
committed
eng-1135 define internal-error
1 parent 9fbe00d commit 6e47ec6

File tree

7 files changed

+131
-106
lines changed

7 files changed

+131
-106
lines changed

apps/roam/src/components/CreateRelationDialog.tsx

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import AutocompleteInput from "roamjs-components/components/AutocompleteInput";
1414
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
1515
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
1616
import getAllPageNames from "roamjs-components/queries/getAllPageNames";
17-
import sendErrorEmail from "~/utils/sendErrorEmail";
1817
import { getSetting } from "~/utils/extensionSettings";
1918
import getDiscourseRelations, {
2019
type DiscourseRelation,
@@ -23,7 +22,7 @@ import { createReifiedRelation } from "~/utils/createReifiedBlock";
2322
import { findDiscourseNodeByTitleAndUid } from "~/utils/findDiscourseNode";
2423
import { getDiscourseNodeFormatInnerExpression } from "~/utils/getDiscourseNodeFormatExpression";
2524
import type { DiscourseNode } from "~/utils/getDiscourseNodes";
26-
import type { Result } from "~/utils/types";
25+
import internalError from "~/utils/internalError";
2726
import getDiscourseNodes from "~/utils/getDiscourseNodes";
2827

2928
export type CreateRelationDialogProps = {
@@ -41,15 +40,6 @@ type ExtendedCreateRelationDialogProps = CreateRelationDialogProps & {
4140
selectedSourceType: DiscourseNode;
4241
};
4342

44-
const internalError = (msg: string) => {
45-
process.env.NODE_ENV === "development"
46-
? console.error(msg)
47-
: void sendErrorEmail({
48-
error: new Error(msg),
49-
type: "Create Relation Dialog Failed",
50-
}).catch(() => {});
51-
};
52-
5343
const CreateRelationDialog = ({
5444
onClose,
5545
sourceNodeUid,
@@ -102,7 +92,11 @@ const CreateRelationDialog = ({
10292
});
10393
if (selectedTargetType === false) {
10494
// should not happen at this point, since the pattern was vetted at input.
105-
internalError("Could not find identify node downstream");
95+
internalError({
96+
type: "create-relation-error",
97+
error:
98+
"Create Relation dialog: Could not find identify node downstream",
99+
});
106100
return null;
107101
}
108102
const candidateRelations = relDataByTag[selectedRelationName].filter(
@@ -122,14 +116,18 @@ const CreateRelationDialog = ({
122116
);
123117
if (candidateRelations.length === 0) {
124118
// also should not happen
125-
internalError("Could not find the relation");
119+
internalError({
120+
type: "create-relation-error",
121+
error: "Create Relation dialog: Could not find the relation",
122+
});
126123
return null;
127124
}
128125
if (candidateRelations.length !== 1) {
129126
// This seems to happen... I need more data.
130-
internalError(
131-
`Too many relations between ${selectedTargetType.type} and ${selectedSourceType.type}: ${candidateRelations.map((r) => r.id).join(",")}`,
132-
);
127+
internalError({
128+
type: "create-relation-error",
129+
error: `Create Relation dialog: Too many relations between ${selectedTargetType.type} and ${selectedSourceType.type}: ${candidateRelations.map((r) => r.id).join(",")}`,
130+
});
133131
return null;
134132
}
135133
return candidateRelations[0];
@@ -276,7 +274,10 @@ const prepareRelData = (
276274
});
277275
if (!nodeSchema) {
278276
// should not happen at this point, since the pattern was vetted at input.
279-
internalError("Could not find identify node downstream");
277+
internalError({
278+
error: "Create Relation dialog: Could not find identify node downstream",
279+
type: "create-relation-error",
280+
});
280281
return [];
281282
}
282283
// note the same relation could be used in both directions

apps/roam/src/components/Export.tsx

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
import calcCanvasNodeSizeAndImg from "~/utils/calcCanvasNodeSizeAndImg";
5555
import { DiscourseNodeShape } from "~/components/canvas/DiscourseNodeUtil";
5656
import { MAX_WIDTH } from "~/components/canvas/Tldraw";
57-
import sendErrorEmail from "~/utils/sendErrorEmail";
57+
import internalError from "~/utils/internalError";
5858
import { getSetting, setSetting } from "~/utils/extensionSettings";
5959

6060
const ExportProgress = ({ id }: { id: string }) => {
@@ -222,7 +222,7 @@ const ExportDialog: ExportDialogComponent = ({
222222
const addToSelectedCanvas = async (pageUid: string) => {
223223
if (typeof results !== "object") return;
224224

225-
let props: Record<string, unknown> = getBlockProps(pageUid);
225+
const props: Record<string, unknown> = getBlockProps(pageUid);
226226

227227
const PADDING_BETWEEN_SHAPES = 20;
228228
const COMMON_BOUNDS_XOFFSET = 250;
@@ -309,10 +309,10 @@ const ExportDialog: ExportDialogComponent = ({
309309
let minY = Number.MAX_SAFE_INTEGER;
310310

311311
shapes.forEach((shape) => {
312-
let rightX = shape.x + shape.w;
313-
let leftX = shape.x;
314-
let topY = shape.y;
315-
let bottomY = shape.y - shape.h;
312+
const rightX = shape.x + shape.w;
313+
const leftX = shape.x;
314+
const topY = shape.y;
315+
const bottomY = shape.y - shape.h;
316316

317317
if (rightX > maxX) maxX = rightX;
318318
if (leftX < minX) minX = leftX;
@@ -468,13 +468,12 @@ const ExportDialog: ExportDialogComponent = ({
468468
id: "query-builder-export-success",
469469
});
470470
} catch (e) {
471-
const error = e as Error;
472-
renderToast({
473-
content: "Looks like there was an error. The team has been notified.",
474-
intent: "danger",
475-
id: "discourse-graphs-error",
471+
internalError({
472+
error: e as Error,
473+
type: "export-error",
474+
userMessage:
475+
"Looks like there was an error. The team has been notified.",
476476
});
477-
sendErrorEmail({ error, type: "Export Dialog Failed" }).catch(() => {});
478477
} finally {
479478
setLoading(false);
480479
onClose();
@@ -688,18 +687,13 @@ const ExportDialog: ExportDialogComponent = ({
688687
setError(`Unsupported export type: ${exportType}`);
689688
}
690689
} catch (e) {
691-
const error = e as Error;
692-
renderToast({
693-
id: "export-error",
694-
content:
690+
internalError({
691+
error: e as Error,
692+
type: "export-error",
693+
userMessage:
695694
"Looks like there was an error. The team has been notified.",
696-
intent: "danger",
697-
});
698-
sendErrorEmail({
699-
error,
700-
type: "Export Dialog Failed",
701695
context: { activeExportType, filename, results },
702-
}).catch(() => {});
696+
});
703697
setDialogOpen(true);
704698
setError((e as Error).message);
705699
} finally {

apps/roam/src/components/canvas/Clipboard.tsx

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import { MAX_WIDTH } from "./Tldraw";
5757
import getBlockProps from "~/utils/getBlockProps";
5858
import setBlockProps from "~/utils/setBlockProps";
5959
import { measureCanvasNodeText } from "~/utils/measureCanvasNodeText";
60-
import sendErrorEmail from "~/utils/sendErrorEmail";
60+
import internalError from "~/utils/internalError";
6161

6262
export type ClipboardPage = {
6363
uid: string;
@@ -79,21 +79,6 @@ const ClipboardContext = createContext<ClipboardContextValue | null>(null);
7979

8080
const CLIPBOARD_PROP_KEY = "pages";
8181

82-
const toError = (error: unknown, fallbackMessage: string): Error => {
83-
if (error instanceof Error) {
84-
return error;
85-
}
86-
if (typeof error === "string") {
87-
return new Error(error);
88-
}
89-
try {
90-
const serialized = JSON.stringify(error);
91-
return new Error(serialized || fallbackMessage);
92-
} catch {
93-
return new Error(fallbackMessage);
94-
}
95-
};
96-
9782
const getOrCreateClipboardBlock = async (
9883
canvasPageTitle: string,
9984
userUid: string,
@@ -155,11 +140,13 @@ export const ClipboardProvider = ({
155140
try {
156141
const userUid = getCurrentUserUid();
157142
if (!userUid) {
158-
sendErrorEmail({
159-
error: new Error("Missing current user UID"),
160-
type: "Canvas Clipboard: Missing current user UID",
161-
context: { canvasPageTitle },
162-
}).catch(() => {});
143+
internalError({
144+
error: new Error("Canvas Clipboard: Missing current user UID"),
145+
type: "canvas-clipboard-missing-uid",
146+
context: {
147+
canvasPageTitle,
148+
},
149+
});
163150
setIsInitialized(true);
164151
return;
165152
}
@@ -179,13 +166,12 @@ export const ClipboardProvider = ({
179166
) {
180167
setPages(storedPages as ClipboardPage[]);
181168
}
182-
} catch (e) {
183-
const normalizedError = toError(e, "Failed to initialize clipboard");
184-
sendErrorEmail({
185-
error: normalizedError,
186-
type: "Canvas Clipboard: Failed to initialize",
169+
} catch (error) {
170+
internalError({
171+
error,
172+
type: "canvas-clipboard-initialization-error",
187173
context: { canvasPageTitle },
188-
}).catch(() => {});
174+
});
189175
} finally {
190176
setIsInitialized(true);
191177
}
@@ -201,13 +187,12 @@ export const ClipboardProvider = ({
201187
setBlockProps(clipboardBlockUid, {
202188
[CLIPBOARD_PROP_KEY]: pages,
203189
});
204-
} catch (e) {
205-
const normalizedError = toError(e, "Failed to persist clipboard state");
206-
sendErrorEmail({
207-
error: normalizedError,
208-
type: "Canvas Clipboard: Failed to persist state",
190+
} catch (error) {
191+
internalError({
192+
error,
193+
type: "canvas-clipboard-state-save-error",
209194
context: { clipboardBlockUid, pageCount: pages.length },
210-
}).catch(() => {});
195+
});
211196
}
212197
}, [pages, clipboardBlockUid, isInitialized]);
213198

@@ -287,8 +272,8 @@ const AddPageModal = ({ isOpen, onClose, onConfirm }: AddPageModalProps) => {
287272
// eslint-disable-next-line @typescript-eslint/await-thenable
288273
const raw = await window.roamAlphaAPI.data.backend.q(
289274
`
290-
[:find ?text ?uid
291-
:where
275+
[:find ?text ?uid
276+
:where
292277
[?e :node/title ?text]
293278
[?e :block/uid ?uid]]`,
294279
);
@@ -447,15 +432,11 @@ const ClipboardPageSection = ({
447432
);
448433
setDiscourseNodes(nodes);
449434
} catch (error) {
450-
const normalizedError = toError(
435+
internalError({
451436
error,
452-
"Failed to fetch discourse nodes",
453-
);
454-
sendErrorEmail({
455-
error: normalizedError,
456-
type: "Canvas Clipboard: Failed to fetch discourse nodes",
437+
type: "canvas-clipboard-discourse-node-error",
457438
context: { pageTitle: page.text },
458-
}).catch(() => {});
439+
});
459440
setDiscourseNodes([]);
460441
} finally {
461442
setIsLoading(false);
@@ -610,11 +591,11 @@ const ClipboardPageSection = ({
610591

611592
const nodeType = findDiscourseNode(node.uid);
612593
if (!nodeType) {
613-
sendErrorEmail({
614-
error: new Error("Node type not found"),
615-
type: "Canvas Clipboard: Node type not found",
594+
internalError({
595+
error: new Error("Canvas Clipboard: Node type not found"),
596+
type: "canvas-clipboard-type-not-found",
616597
context: { uid: node.uid },
617-
}).catch(() => {});
598+
});
618599
return;
619600
}
620601

apps/roam/src/components/canvas/Tldraw.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,12 @@ import { createMigrations } from "./DiscourseRelationShape/discourseRelationMigr
8686
import ToastListener, { dispatchToastEvent } from "./ToastListener";
8787
import { CanvasDrawerPanel } from "./CanvasDrawer";
8888
import { ClipboardPanel, ClipboardProvider } from "./Clipboard";
89-
import sendErrorEmail from "~/utils/sendErrorEmail";
89+
import internalError from "~/utils/internalError";
9090
import { AUTO_CANVAS_RELATIONS_KEY } from "~/data/userSettings";
9191
import { getSetting } from "~/utils/extensionSettings";
9292
import { isPluginTimerReady, waitForPluginTimer } from "~/utils/pluginTimer";
9393
import { HistoryEntry } from "@tldraw/store";
9494
import { TLRecord } from "@tldraw/tlschema";
95-
import getCurrentUserDisplayName from "roamjs-components/queries/getCurrentUserDisplayName";
9695

9796
declare global {
9897
interface Window {
@@ -509,16 +508,14 @@ const TldrawCanvas = ({ title }: { title: string }) => {
509508
error.stack = e.detail.stack;
510509
}
511510

512-
sendErrorEmail({
511+
internalError({
513512
error,
514-
type: "Tldraw Error",
513+
type: "tldraw-error",
515514
context: {
516515
title: title,
517516
lastActions: lastActionsRef.current,
518517
},
519-
}).catch(() => {});
520-
521-
console.error("Tldraw Error:", e.detail);
518+
});
522519
};
523520

524521
document.addEventListener(

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import {
2525
} from "tldraw";
2626
import { AddPullWatch } from "roamjs-components/types";
2727
import { LEGACY_SCHEMA } from "~/data/legacyTldrawSchema";
28-
import sendErrorEmail from "~/utils/sendErrorEmail";
29-
import getCurrentUserDisplayName from "roamjs-components/queries/getCurrentUserDisplayName";
28+
import internalError from "~/utils/internalError";
3029

3130
const THROTTLE = 350;
3231

@@ -142,21 +141,20 @@ export const useRoamStore = ({
142141
error: Error;
143142
errorMessage: string;
144143
}): void => {
145-
console.error(errorMessage, error);
146144
setError(error);
147145
setLoading(false);
148146
const snapshotSize = initialSnapshot
149147
? JSON.stringify(initialSnapshot).length
150148
: 0;
151-
sendErrorEmail({
149+
internalError({
152150
error,
153-
type: errorMessage,
151+
type: "roam-store-error",
154152
context: {
155153
pageUid,
156154
snapshotSize,
157155
...(snapshotSize < 10000 ? { initialSnapshot } : {}),
158156
},
159-
}).catch(() => {});
157+
});
160158
};
161159

162160
// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -305,14 +303,13 @@ export const useRoamStore = ({
305303
setNeedsUpgrade(false);
306304
setInitialSnapshot(null);
307305
setError(error);
308-
sendErrorEmail({
306+
internalError({
309307
error,
310-
type: "Failed to perform Canvas upgrade",
308+
type: "canvas-upgrade-error",
311309
context: {
312310
data: { oldData },
313311
},
314-
}).catch(() => {});
315-
console.error("Failed to perform Canvas upgrade", error);
312+
});
316313
}
317314
};
318315

0 commit comments

Comments
 (0)