Skip to content

Commit 6715573

Browse files
committed
catchしたエラーとチャットエラーをsentryに送信
1 parent ac0474c commit 6715573

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

app/(docs)/@docs/[lang]/[pageId]/chatForm.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
"use client";
22

3-
import { useState, FormEvent, useEffect, useRef, useCallback, useMemo } from "react";
3+
import {
4+
useState,
5+
FormEvent,
6+
useEffect,
7+
useRef,
8+
useCallback,
9+
useMemo,
10+
} from "react";
411
// import useSWR from "swr";
512
// import {
613
// getQuestionExample,
@@ -13,6 +20,7 @@ import { usePathname, useRouter } from "next/navigation";
1320
import { ChatStreamEvent } from "@/api/chat/route";
1421
import { useStreamingChatContext } from "@/(docs)/streamingChatContext";
1522
import { revalidateChatAction } from "@/actions/revalidateChat";
23+
import { captureException } from "@sentry/nextjs";
1624

1725
interface ChatFormProps {
1826
path: PagePath;
@@ -84,7 +92,6 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {
8492
}, [exampleChoice]);
8593

8694
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
87-
8895
let userQuestion = inputValue;
8996
if (!userQuestion && exampleData.length > 0 && exampleChoice) {
9097
// 質問が空欄なら、質問例を使用
@@ -114,7 +121,8 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {
114121
execResults,
115122
}),
116123
});
117-
} catch {
124+
} catch (e) {
125+
captureException(e);
118126
setErrorMessage("AIへの接続に失敗しました");
119127
setIsLoading(false);
120128
return;
@@ -184,12 +192,14 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {
184192
streamingChatContext.finishStreaming();
185193
router.refresh();
186194
}
187-
} catch {
195+
} catch (e) {
196+
captureException(e);
188197
// ignore JSON parse errors
189198
}
190199
}
191200
}
192201
} catch (err) {
202+
captureException(err);
193203
console.error("Stream reading failed:", err);
194204
// ナビゲーション後のエラーはストリーミングを終了してローディングを止める
195205
if (!navigated) {

app/api/chat/route.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ReplOutputSchema,
1919
} from "@my-code/runtime/interface";
2020
import { z } from "zod";
21+
import { captureException } from "@sentry/nextjs";
2122

2223
const ChatParamsSchema = z.object({
2324
path: PagePathSchema,
@@ -209,9 +210,9 @@ export async function POST(request: NextRequest) {
209210
function send(event: ChatStreamEvent) {
210211
controller.enqueue(encoder.encode(JSON.stringify(event) + "\n"));
211212
}
213+
let fullText = "";
212214

213215
try {
214-
let fullText = "";
215216
let headerParsed = false;
216217
let chatId: string | undefined;
217218
let contentAfterHeader = "";
@@ -244,6 +245,15 @@ export async function POST(request: NextRequest) {
244245
type: "error",
245246
message: "AIからの応答にタイトルが含まれていませんでした",
246247
});
248+
captureException(
249+
"AIからの応答にタイトルが含まれていませんでした",
250+
{
251+
extra: {
252+
prompt,
253+
fullText,
254+
},
255+
}
256+
);
247257
controller.close();
248258
return;
249259
}
@@ -286,6 +296,12 @@ export async function POST(request: NextRequest) {
286296
type: "error",
287297
message: "AIからの応答の形式が正しくありませんでした",
288298
});
299+
captureException("AIからの応答の形式が正しくありませんでした", {
300+
extra: {
301+
prompt,
302+
fullText,
303+
},
304+
});
289305
controller.close();
290306
return;
291307
}
@@ -321,6 +337,7 @@ export async function POST(request: NextRequest) {
321337
send({ type: "done" });
322338
controller.close();
323339
} catch (error: unknown) {
340+
captureException(error, { extra: { prompt, fullText } });
324341
console.error("Error in AI streaming:", error);
325342
try {
326343
controller.enqueue(

0 commit comments

Comments
 (0)