Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(ui): add client-side error notifications (#2166)

Richard Krümmel078ceb7ff186
diff --git a/web/src/components/star-toggle.tsx b/web/src/components/star-toggle.tsx
index d813e0ac55..985340e400 100644
--- a/web/src/components/star-toggle.tsx
+++ b/web/src/components/star-toggle.tsx
@@ -7,6 +7,7 @@ import { cn } from "@/src/utils/tailwind";
import { type RouterOutput, type RouterInput } from "@/src/utils/types";
import { useState } from "react";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
+import { trpcErrorToast } from "@/src/utils/trpcErrorToast";
export function StarToggle({
value,
@@ -76,7 +77,7 @@ export function StarTraceToggle({
onError: (err, _newTodo, context) => {
setIsLoading(false);
// Rollback to the previous value if mutation fails
- console.log("error", err);
+ trpcErrorToast(err);
utils.traces.all.setData(tracesFilter, context?.prev);
},
onSettled: () => {
@@ -157,7 +158,7 @@ export function StarTraceDetailsToggle({
},
onError: (err, _newTodo, context) => {
setIsLoading(false);
- console.log("error", err);
+ trpcErrorToast(err);
// Rollback to the previous value if mutation fails
utils.traces.byId.setData({ traceId }, context?.prevData);
},
diff --git a/web/src/components/ui/button.tsx b/web/src/components/ui/button.tsx
index c0973a8e6a..c2161ece52 100644
--- a/web/src/components/ui/button.tsx
+++ b/web/src/components/ui/button.tsx
@@ -19,6 +19,8 @@ const buttonVariants = cva(
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
+ errorNotification:
+ "bg-destructive-foreground/90 text-destructive hover:bg-destructive-foreground/80",
},
size: {
default: "h-10 px-4 py-2",
diff --git a/web/src/features/notifications/ErrorNotification.tsx b/web/src/features/notifications/ErrorNotification.tsx
new file mode 100644
index 0000000000..a3b35aeca4
--- /dev/null
+++ b/web/src/features/notifications/ErrorNotification.tsx
@@ -0,0 +1,79 @@
+import { Button } from "@/src/components/ui/button";
+import { AlertTriangle, X } from "lucide-react";
+import {
+ chatAvailable,
+ sendUserChatMessage,
+} from "@/src/features/support-chat/chat";
+
+interface ErrorNotificationProps {
+ error: string;
+ description: string;
+ cause?: string;
+ dismissToast: (t?: string | number | undefined) => void;
+ toast: string | number;
+ path?: string;
+}
+
+export const ErrorNotification: React.FC<ErrorNotificationProps> = ({
+ error,
+ description,
+ cause,
+ dismissToast,
+ toast,
+ path,
+}) => {
+ const handleReportIssueClick = () => {
+ if (chatAvailable) {
+ const currentUrl = window.location.href;
+ const message = `I received the following error:\n\nError: ${error}\nDescription: ${description}\n ${cause ? `Cause: ${cause}\n` : ""}${path ? `Path: ${path}\n` : ""}URL: ${currentUrl}`;
+ sendUserChatMessage(message);
+ dismissToast(toast);
+ }
+ };
+
+ return (
+ <div className="flex justify-between">
+ <div className="flex min-w-[300px] flex-1 flex-col gap-2">
+ <div className="flex items-center gap-2">
+ <AlertTriangle size={20} className="text-destructive-foreground" />
+ <div className="m-0 text-sm font-medium leading-tight text-destructive-foreground">
+ {error}
+ </div>
+ </div>
+ {description && (
+ <div className="text-sm leading-tight text-destructive-foreground">
+ {description}
+ </div>
+ )}
+ {path && (
+ <div className="text-sm leading-tight text-destructive-foreground">
+ Path: {path}
+ </div>
+ )}
+ {cause && (
+ <div className="max-h-32 overflow-y-auto text-sm leading-tight text-destructive-foreground">
+ {cause}
+ </div>
+ )}
+ {chatAvailable && (
+ <Button
+ variant="errorNotification"
+ size={"sm"}
+ onClick={() => {
+ handleReportIssueClick();
+ }}
+ >
+ Report issue to Langfuse team
+ </Button>
+ )}
+ </div>
+ <button
+ className="flex h-6 w-6 cursor-pointer items-start justify-end border-none bg-transparent p-0 text-destructive-foreground transition-colors duration-200"
+ onClick={() => dismissToast(toast)}
+ aria-label="Close"
+ >
+ <X size={14} />
+ </button>
+ </div>
+ );
+};
diff --git a/web/src/features/notifications/Notification.tsx b/web/src/features/notifications/Notification.tsx
index d8591f2ea6..55139bf32b 100644
--- a/web/src/features/notifications/Notification.tsx
+++ b/web/src/features/notifications/Notification.tsx
@@ -15,7 +15,7 @@ interface NotificationProps {
toast: string | number;
}
-const Notification: React.FC<NotificationProps> = ({
+export const Notification: React.FC<NotificationProps> = ({
notification,
setLastSeenId,
dismissToast,
@@ -50,5 +50,3 @@ const Notification: React.FC<NotificationProps> = ({
</div>
);
};
-
-export default Notification;
diff --git a/web/src/features/notifications/checkNotifications.tsx b/web/src/features/notifications/checkNotifications.tsx
index 68775b3456..ac9681d64a 100644
--- a/web/src/features/notifications/checkNotifications.tsx
+++ b/web/src/features/notifications/checkNotifications.tsx
@@ -2,7 +2,8 @@ import { useEffect } from "react";
import Link from "next/link";
import { toast } from "sonner";
import useLocalStorage from "@/src/components/useLocalStorage";
-import Notification, {
+import {
+ Notification,
type TNotification,
} from "@/src/features/notifications/Notification";
import { Button } from "@/src/components/ui/button";
diff --git a/web/src/features/notifications/showErrorToast.tsx b/web/src/features/notifications/showErrorToast.tsx
new file mode 100644
index 0000000000..7c7ba23d1b
--- /dev/null
+++ b/web/src/features/notifications/showErrorToast.tsx
@@ -0,0 +1,31 @@
+import { toast } from "sonner";
+import { ErrorNotification } from "@/src/features/notifications/ErrorNotification";
+
+export const showErrorToast = (
+ error: string,
+ description: string,
+ cause?: string,
+ path?: string,
+) => {
+ toast.custom(
+ (t) => (
+ <ErrorNotification
+ error={error}
+ description={description}
+ cause={cause}
+ path={path}
+ dismissToast={toast.dismiss}
+ toast={t}
+ />
+ ),
+ {
+ duration: Infinity,
+ style: {
+ padding: "1rem",
+ border: "1px solid hsl(var(--destructive))",
+ borderRadius: "0.5rem",
+ backgroundColor: "hsl(var(--destructive))",
+ },
+ },
+ );
+};
diff --git a/web/src/features/tag/components/TagPromptDetailsPopover.tsx b/web/src/features/tag/components/TagPromptDetailsPopover.tsx
index af416f41c0..4b13f55b0a 100644
--- a/web/src/features/tag/components/TagPromptDetailsPopover.tsx
+++ b/web/src/features/tag/components/TagPromptDetailsPopover.tsx
@@ -3,6 +3,7 @@ import { api } from "@/src/utils/api";
import { useHasAccess } from "@/src/features/rbac/utils/checkAccess";
import { type RouterOutput } from "@/src/utils/types";
import TagManager from "@/src/features/tag/components/TagMananger";
+import { trpcErrorToast } from "@/src/utils/trpcErrorToast";
type TagPromptDetailsPopoverProps = {
tags: string[];
@@ -34,6 +35,7 @@ export function TagPromptDetailsPopover({
},
onError: (err, _newTags, context) => {
setIsLoading(false);
+ trpcErrorToast(err);
// Rollback to the previous value if mutation fails
utils.prompts.allVersions.setData(
{ projectId: projectId, name: promptName },
diff --git a/web/src/features/tag/components/TagPromptPopover.tsx b/web/src/features/tag/components/TagPromptPopover.tsx
index e6edf2acc1..ff060fcf06 100644
--- a/web/src/features/tag/components/TagPromptPopover.tsx
+++ b/web/src/features/tag/components/TagPromptPopover.tsx
@@ -3,6 +3,7 @@ import { api } from "@/src/utils/api";
import { useHasAccess } from "@/src/features/rbac/utils/checkAccess";
import { type RouterOutput, type RouterInput } from "@/src/utils/types";
import TagManager from "@/src/features/tag/components/TagMananger";
+import { trpcErrorToast } from "@/src/utils/trpcErrorToast";
type TagPromptPopverProps = {
tags: string[];
@@ -32,7 +33,7 @@ export function TagPromptPopover({
},
onError: (err, _newTags, context) => {
utils.prompts.all.setData(promptsFilter, context?.prevPrompt);
- console.error(err);
+ trpcErrorToast(err);
setIsLoading(false);
},
onSettled: (data, error, { name, tags }) => {
diff --git a/web/src/features/tag/components/TagTraceDetailsPopover.tsx b/web/src/features/tag/components/TagTraceDetailsPopover.tsx
index cc3da8d9bc..ec583aed7c 100644
--- a/web/src/features/tag/components/TagTraceDetailsPopover.tsx
+++ b/web/src/features/tag/components/TagTraceDetailsPopover.tsx
@@ -3,6 +3,7 @@ import { api } from "@/src/utils/api";
import { useHasAccess } from "@/src/features/rbac/utils/checkAccess";
import { type RouterOutput } from "@/src/utils/types";
import TagManager from "@/src/features/tag/components/TagMananger";
+import { trpcErrorToast } from "@/src/utils/trpcErrorToast";
type TagTraceDetailsPopoverProps = {
tags: string[];
@@ -32,6 +33,7 @@ export function TagTraceDetailsPopover({
},
onError: (err, _newTags, context) => {
setIsLoading(false);
+ trpcErrorToast(err);
// Rollback to the previous value if mutation fails
utils.traces.byId.setData({ traceId }, context?.prev);
},
diff --git a/web/src/features/tag/components/TagTracePopver.tsx b/web/src/features/tag/components/TagTracePopver.tsx
index fba9de6d14..54c1dc8be7 100644
--- a/web/src/features/tag/components/TagTracePopver.tsx
+++ b/web/src/features/tag/components/TagTracePopver.tsx
@@ -3,6 +3,7 @@ import { api } from "@/src/utils/api";
import { useHasAccess } from "@/src/features/rbac/utils/checkAccess";
import { type RouterOutput, type RouterInput } from "@/src/utils/types";
import TagManager from "@/src/features/tag/components/TagMananger";
+import { trpcErrorToast } from "@/src/utils/trpcErrorToast";
type TagTracePopoverProps = {
tags: string[];
@@ -32,7 +33,7 @@ export function TagTracePopover({
},
onError: (err, _newTags, context) => {
utils.traces.all.setData(tracesFilter, context?.prevTrace);
- console.log("error", err);
+ trpcErrorToast(err);
setIsLoading(false);
},
onSettled: (data, error, { traceId, tags }) => {
diff --git a/web/src/utils/api.ts b/web/src/utils/api.ts
index cd0dadc860..d4481b285c 100644
--- a/web/src/utils/api.ts
+++ b/web/src/utils/api.ts
@@ -17,6 +17,7 @@ import superjson from "superjson";
import { type AppRouter } from "@/src/server/api/root";
import { setUpSuperjson } from "@/src/utils/superjson";
+import { trpcErrorToast } from "@/src/utils/trpcErrorToast";
setUpSuperjson();
@@ -64,6 +65,16 @@ export const api = createTRPCNext<AppRouter>({
}),
}),
],
+ queryClientConfig: {
+ defaultOptions: {
+ queries: {
+ onError: (error) => trpcErrorToast(error),
+ },
+ mutations: {
+ onError: (error) => trpcErrorToast(error),
+ },
+ },
+ },
};
},
/**
diff --git a/web/src/utils/trpcErrorToast.tsx b/web/src/utils/trpcErrorToast.tsx
new file mode 100644
index 0000000000..6bd2efb214
--- /dev/null
+++ b/web/src/utils/trpcErrorToast.tsx
@@ -0,0 +1,34 @@
+import { TRPCClientError } from "@trpc/client";
+import { showErrorToast } from "@/src/features/notifications/showErrorToast";
+
+const errorTitleMap: Record<string, string> = {
+ BAD_REQUEST: "Bad Request",
+ UNAUTHORIZED: "Unauthorized",
+ FORBIDDEN: "Forbidden",
+ NOT_FOUND: "Not Found",
+ TIMEOUT: "Timeout",
+ CONFLICT: "Conflict",
+ PRECONDITION_FAILED: "Precondition Failed",
+ PAYLOAD_TOO_LARGE: "Payload Too Large",
+ METHOD_NOT_SUPPORTED: "Method Not Supported",
+ UNPROCESSABLE_CONTENT: "Unprocessable Content",
+ TOO_MANY_REQUESTS: "Too Many Requests",
+ CLIENT_CLOSED_REQUEST: "Client Closed Request",
+ INTERNAL_SERVER_ERROR: "Internal Server Error",
+};
+
+export const trpcErrorToast = (error: unknown) => {
+ if (error instanceof TRPCClientError) {
+ const path = error.data?.path;
+ const cause = error.data?.cause;
+ const description = error.message;
+ const errorTitle = errorTitleMap[error.data?.code] || "Unexpected Error";
+
+ showErrorToast(errorTitle, description, cause, path);
+ } else {
+ showErrorToast(
+ "Unexpected Error",
+ "An unexpected error occurred. Please try again.",
+ );
+ }
+};