Skip to content

Feat/capture sentry errors #108

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

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/hooks/use-toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as React from "react";

import type { ToastActionElement, ToastProps } from "@/components/ui/toast";

const TOAST_LIMIT = 1;
const TOAST_LIMIT = 3;
const TOAST_REMOVE_DELAY = 1000000;

type ToasterToast = ToastProps & {
Expand Down
41 changes: 30 additions & 11 deletions src/hooks/useChat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState, useEffect, useCallback, useRef } from "react";
import { useToast } from "@/hooks/use-toast";
import { supabase } from "@/utils/supabase/client";
import * as Sentry from "@sentry/nextjs";

export interface Message {
role: "user" | "assistant";
Expand Down Expand Up @@ -64,6 +65,7 @@ export function useChat() {
});
} catch (error) {
console.error("Failed to reset chat history:", error);
Sentry.captureException(error);
toast({
title: "Error",
description: "Failed to reset chat history.",
Expand Down Expand Up @@ -144,6 +146,7 @@ export function useChat() {
}
} catch (error) {
console.error("Failed to fetch history:", error);
Sentry.captureException(error);
toast({
title: "Error",
description: "Failed to load chat history.",
Expand Down Expand Up @@ -233,21 +236,36 @@ export function useChat() {
setIsLoading(false);
}
} catch (error) {
Sentry.captureException(error);
console.error("Error parsing SSE data:", error);
}
};

eventSource.onerror = () => {
if (isLoading) {
setIsLoading(false);
} else {
console.error("EventSource failed");
toast({
title: "Connection Failed",
description: "There was a problem with the connection. Please try again.",
variant: "destructive",
});
}
eventSource.addEventListener("error", (event: MessageEvent) => {
const errorData = JSON.parse(event.data);
console.error("Received server error:", errorData.message);

toast({
title: "Server Error",
description: errorData.message,
variant: "destructive",
});

Sentry.captureException(new Error(errorData.message));
eventSource.close();
setIsLoading(false);
});

eventSource.onerror = (error: Event) => {
console.error("EventSource encountered a generic error:", error);
toast({
title: "Connection Error",
description: "A connection error occurred. Please try again.",
variant: "destructive",
});

Sentry.captureException(error);
setIsLoading(false);
};

eventSource.addEventListener("finish", () => {
Expand All @@ -264,6 +282,7 @@ export function useChat() {
});
} catch (error) {
console.error("Error sending message:", error);
Sentry.captureException(error);
toast({
title: "Error",
description: error instanceof Error ? error.message : "Failed to send message",
Expand Down
40 changes: 29 additions & 11 deletions src/hooks/useCrewChat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState, useEffect, useCallback, useRef } from "react";
import { useToast } from "@/hooks/use-toast";
import { supabase } from "@/utils/supabase/client";
import * as Sentry from "@sentry/nextjs";

export interface Message {
role: "user" | "assistant";
Expand Down Expand Up @@ -37,6 +38,7 @@ export function useCrewChat() {
});
} catch (error) {
console.error("Failed to reset chat history:", error);
Sentry.captureException(error);
toast({
title: "Error",
description: "Failed to reset chat history.",
Expand Down Expand Up @@ -123,21 +125,36 @@ export function useCrewChat() {
setIsLoading(false);
}
} catch (error) {
Sentry.captureException(error);
console.error("Error parsing SSE data:", error);
}
};

eventSource.onerror = () => {
if (isLoading) {
setIsLoading(false);
} else {
console.error("EventSource failed");
toast({
title: "Connection Failed",
description: "There was a problem with the connection. Please try again.",
variant: "destructive",
});
}
eventSource.addEventListener("error", (event: MessageEvent) => {
const errorData = JSON.parse(event.data);
console.error("Received server error:", errorData.message);

toast({
title: "Server Error",
description: errorData.message,
variant: "destructive",
});

Sentry.captureException(new Error(errorData.message));
eventSource.close();
setIsLoading(false);
});

eventSource.onerror = (error: Event) => {
console.error("EventSource encountered a generic error:", error);
toast({
title: "Connection Error",
description: "A connection error occurred. Please try again.",
variant: "destructive",
});

Sentry.captureException(error);
setIsLoading(false);
};

eventSource.addEventListener("finish", () => {
Expand All @@ -153,6 +170,7 @@ export function useCrewChat() {
setMessages((prev) => [...prev, assistantMessage]);
});
} catch (error) {
Sentry.captureException(error);
console.error("Error sending message:", error);
toast({
title: "Error",
Expand Down
Loading