-
Notifications
You must be signed in to change notification settings - Fork 2
feat: error toasts #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jonathanlab
wants to merge
3
commits into
main
Choose a base branch
from
feat/error-toasts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: error toasts #368
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { ipcMain } from "electron"; | ||
| import { logger } from "./logger.js"; | ||
|
|
||
| export function initializeMainErrorHandling(): void { | ||
| process.on("uncaughtException", (error) => { | ||
| logger.error("Uncaught exception", error); | ||
| }); | ||
|
|
||
| process.on("unhandledRejection", (reason) => { | ||
| const error = reason instanceof Error ? reason : new Error(String(reason)); | ||
| logger.error("Unhandled rejection", error); | ||
| }); | ||
|
|
||
| ipcMain.on( | ||
| "preload-error", | ||
| (_, error: { message: string; stack?: string }) => { | ||
| logger.error("Preload error", error); | ||
| }, | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,15 @@ | ||
| import type { Logger, ScopedLogger } from "@shared/lib/create-logger.js"; | ||
| import { createLogger } from "@shared/lib/create-logger.js"; | ||
| import { app } from "electron"; | ||
| import log from "electron-log/main"; | ||
|
|
||
| // Initialize IPC transport to forward main process logs to renderer dev tools | ||
| log.initialize(); | ||
|
|
||
| // Set levels - use debug in dev (check NODE_ENV since app.isPackaged may not be ready) | ||
| const isDev = process.env.NODE_ENV === "development" || !app.isPackaged; | ||
| const level = isDev ? "debug" : "info"; | ||
| log.transports.file.level = level; | ||
| log.transports.console.level = level; | ||
| // IPC transport needs level set separately | ||
| log.transports.ipc.level = level; | ||
|
|
||
| export const logger = { | ||
| info: (message: string, ...args: unknown[]) => log.info(message, ...args), | ||
| warn: (message: string, ...args: unknown[]) => log.warn(message, ...args), | ||
| error: (message: string, ...args: unknown[]) => log.error(message, ...args), | ||
| debug: (message: string, ...args: unknown[]) => log.debug(message, ...args), | ||
|
|
||
| scope: (name: string) => { | ||
| const scoped = log.scope(name); | ||
| return { | ||
| info: (message: string, ...args: unknown[]) => | ||
| scoped.info(message, ...args), | ||
| warn: (message: string, ...args: unknown[]) => | ||
| scoped.warn(message, ...args), | ||
| error: (message: string, ...args: unknown[]) => | ||
| scoped.error(message, ...args), | ||
| debug: (message: string, ...args: unknown[]) => | ||
| scoped.debug(message, ...args), | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| export type Logger = typeof logger; | ||
| export type ScopedLogger = ReturnType<typeof logger.scope>; | ||
| export const logger = createLogger(log); | ||
| export type { Logger, ScopedLogger }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,23 @@ | ||
| import { ipcRenderer } from "electron"; | ||
| import { exposeElectronTRPC } from "trpc-electron/main"; | ||
| import "electron-log/preload"; | ||
|
|
||
| // No TRPC available, so just use IPC | ||
| process.on("uncaughtException", (error) => { | ||
| ipcRenderer.send("preload-error", { | ||
| message: error.message, | ||
| stack: error.stack, | ||
| }); | ||
| }); | ||
|
|
||
| process.on("unhandledRejection", (reason) => { | ||
| const error = reason instanceof Error ? reason : new Error(String(reason)); | ||
| ipcRenderer.send("preload-error", { | ||
| message: error.message, | ||
| stack: error.stack, | ||
| }); | ||
| }); | ||
|
|
||
| process.once("loaded", async () => { | ||
| exposeElectronTRPC(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| export const UserNotificationEvent = { | ||
| Notify: "notify", | ||
| } as const; | ||
|
|
||
| export type NotificationSeverity = "error" | "warning" | "info"; | ||
|
|
||
| export interface UserNotificationPayload { | ||
| severity: NotificationSeverity; | ||
| title: string; | ||
| description?: string; | ||
| } | ||
|
|
||
| export interface UserNotificationEvents { | ||
| [UserNotificationEvent.Notify]: UserNotificationPayload; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { app } from "electron"; | ||
| import { injectable, postConstruct } from "inversify"; | ||
| import { logger } from "../../lib/logger.js"; | ||
| import { TypedEventEmitter } from "../../lib/typed-event-emitter.js"; | ||
| import { | ||
| UserNotificationEvent, | ||
| type UserNotificationEvents, | ||
| } from "./schemas.js"; | ||
|
|
||
| const isDev = process.env.NODE_ENV === "development" || !app.isPackaged; | ||
| const devErrorToastsEnabled = | ||
| isDev && process.env.VITE_DEV_ERROR_TOASTS !== "false"; | ||
|
|
||
| @injectable() | ||
| export class UserNotificationService extends TypedEventEmitter<UserNotificationEvents> { | ||
| @postConstruct() | ||
| init(): void { | ||
| if (devErrorToastsEnabled) { | ||
| logger.setDevToastEmitter((title, desc) => this.error(title, desc)); | ||
| } | ||
| } | ||
|
|
||
| error(title: string, description?: string): void { | ||
| this.emit(UserNotificationEvent.Notify, { | ||
| severity: "error", | ||
| title, | ||
| description, | ||
| }); | ||
| } | ||
|
|
||
| warning(title: string, description?: string): void { | ||
| this.emit(UserNotificationEvent.Notify, { | ||
| severity: "warning", | ||
| title, | ||
| description, | ||
| }); | ||
| } | ||
|
|
||
| info(title: string, description?: string): void { | ||
| this.emit(UserNotificationEvent.Notify, { | ||
| severity: "info", | ||
| title, | ||
| description, | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { container } from "../../di/container.js"; | ||
| import { MAIN_TOKENS } from "../../di/tokens.js"; | ||
| import { UserNotificationEvent } from "../../services/user-notification/schemas.js"; | ||
| import type { UserNotificationService } from "../../services/user-notification/service.js"; | ||
| import { publicProcedure, router } from "../trpc.js"; | ||
|
|
||
| const getService = () => | ||
| container.get<UserNotificationService>(MAIN_TOKENS.UserNotificationService); | ||
|
|
||
| export const userNotificationRouter = router({ | ||
| onNotify: publicProcedure.subscription(async function* (opts) { | ||
| const service = getService(); | ||
| const iterable = service.toIterable(UserNotificationEvent.Notify, { | ||
| signal: opts.signal, | ||
| }); | ||
| for await (const data of iterable) { | ||
| yield data; | ||
| } | ||
| }), | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
apps/array/src/api/posthogClient.ts → apps/array/src/renderer/api/posthogClient.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { Button, Card, Flex, Text } from "@radix-ui/themes"; | ||
| import { logger } from "@renderer/lib/logger"; | ||
| import type { ReactNode } from "react"; | ||
| import { ErrorBoundary as ReactErrorBoundary } from "react-error-boundary"; | ||
|
|
||
| interface Props { | ||
| children: ReactNode; | ||
| fallback?: ReactNode; | ||
| } | ||
|
|
||
| function DefaultFallback({ | ||
| error, | ||
| onReset, | ||
| }: { | ||
| error: Error; | ||
| onReset: () => void; | ||
| }) { | ||
| return ( | ||
| <Flex align="center" justify="center" minHeight="100vh" p="4"> | ||
| <Card size="3" style={{ maxWidth: 400 }}> | ||
| <Flex direction="column" align="center" gap="4" p="4"> | ||
| <Text size="3" weight="bold" align="center"> | ||
| Something went wrong | ||
| </Text> | ||
| <Text size="2" color="gray" align="center"> | ||
| {error.message} | ||
| </Text> | ||
| <Button onClick={onReset} variant="soft"> | ||
| Try again | ||
| </Button> | ||
| </Flex> | ||
| </Card> | ||
| </Flex> | ||
| ); | ||
| } | ||
|
|
||
| export function ErrorBoundary({ children, fallback }: Props) { | ||
| return ( | ||
| <ReactErrorBoundary | ||
| fallbackRender={({ error, resetErrorBoundary }) => | ||
| fallback ?? ( | ||
| <DefaultFallback error={error} onReset={resetErrorBoundary} /> | ||
| ) | ||
| } | ||
| onError={(error, info) => { | ||
| logger.error("React error boundary caught error", { | ||
| error: error.message, | ||
| stack: error.stack, | ||
| componentStack: info.componentStack, | ||
| }); | ||
| }} | ||
| > | ||
| {children} | ||
| </ReactErrorBoundary> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { trpcReact } from "@renderer/trpc/client"; | ||
| import { toast } from "@utils/toast"; | ||
|
|
||
| export function useUserNotifications() { | ||
| trpcReact.userNotification.onNotify.useSubscription(undefined, { | ||
| onData: ({ severity, title, description }) => { | ||
| switch (severity) { | ||
| case "error": | ||
| toast.error(title, { description }); | ||
| break; | ||
| case "warning": | ||
| toast.warning(title, { description }); | ||
| break; | ||
| case "info": | ||
| toast.info(title, description); | ||
| break; | ||
| } | ||
| }, | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should just render the error in the screen instead of a toast if it triggers the error boundary.