Skip to content

Commit 3ab209e

Browse files
committed
chore: improved error ts
1 parent 5437e43 commit 3ab209e

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

chat/src/components/chat-provider.tsx

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
useContext,
1111
} from "react";
1212
import { toast } from "sonner";
13+
import {getErrorMessage} from "@/lib/error-utils";
1314

1415
interface Message {
1516
id: number;
@@ -34,6 +35,22 @@ interface StatusChangeEvent {
3435
status: string;
3536
}
3637

38+
interface APIErrorDetail {
39+
location: string;
40+
message: string;
41+
value: null | string | number | boolean | object;
42+
}
43+
44+
interface APIErrorModel {
45+
$schema: string;
46+
detail: string;
47+
errors: APIErrorDetail[];
48+
instance: string;
49+
status: number;
50+
title: string;
51+
type: string;
52+
}
53+
3754
function isDraftMessage(message: Message | DraftMessage): boolean {
3855
return message.id === undefined;
3956
}
@@ -235,34 +252,27 @@ export function ChatProvider({ children }: PropsWithChildren) {
235252
});
236253

237254
if (!response.ok) {
238-
const errorData = await response.json();
255+
const errorData = await response.json() as APIErrorModel;
239256
console.error("Failed to send message:", errorData);
240257
const detail = errorData.detail;
241258
const messages =
242259
"errors" in errorData
243-
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
244-
errorData.errors.map((e: any) => e.message).join(", ")
260+
?
261+
errorData.errors.map((e: APIErrorDetail) => e.message).join(", ")
245262
: "";
246263

247264
const fullDetail = `${detail}: ${messages}`;
248265
toast.error(`Failed to send message`, {
249266
description: fullDetail,
250267
});
251268
}
252-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
253-
} catch (error: any) {
254-
console.error("Error sending message:", error);
255-
const detail = error.detail;
256-
const messages =
257-
"errors" in error
258-
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
259-
error.errors.map((e: any) => e.message).join("\n")
260-
: "";
261269

262-
const fullDetail = `${detail}: ${messages}`;
270+
} catch (error) {
271+
console.error("Error sending message:", error);
272+
const message = getErrorMessage(error)
263273

264274
toast.error(`Error sending message`, {
265-
description: fullDetail,
275+
description: message,
266276
});
267277
} finally {
268278
if (type === "user") {
@@ -285,13 +295,13 @@ export function ChatProvider({ children }: PropsWithChildren) {
285295

286296
if (!response.ok) {
287297
result.ok = false;
288-
const errorData = await response.json();
298+
const errorData = await response.json() as APIErrorModel;
289299
console.error("Failed to send message:", errorData);
290300
const detail = errorData.detail;
291301
const messages =
292302
"errors" in errorData
293-
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
294-
errorData.errors.map((e: any) => e.message).join(", ")
303+
?
304+
errorData.errors.map((e: APIErrorDetail) => e.message).join(", ")
295305
: "";
296306

297307
const fullDetail = `${detail}: ${messages}`;
@@ -301,21 +311,14 @@ export function ChatProvider({ children }: PropsWithChildren) {
301311
} else {
302312
result = (await response.json()) as FileUploadResponse;
303313
}
304-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
305-
} catch (error: any) {
314+
315+
} catch (error) {
306316
result.ok = false;
307317
console.error("Error uploading files:", error);
308-
const detail = error.detail;
309-
const messages =
310-
"errors" in error
311-
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
312-
error.errors.map((e: any) => e.message).join("\n")
313-
: "";
314-
315-
const fullDetail = `${detail}: ${messages}`;
318+
const message = getErrorMessage(error)
316319

317320
toast.error(`Error uploading files`, {
318-
description: fullDetail,
321+
description: message,
319322
});
320323
}
321324
return result;

chat/src/components/message-input.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import TextareaAutosize from "react-textarea-autosize";
1919
import {useChat} from "./chat-provider";
2020
import {DragDrop} from "./drag-drop";
2121
import {toast} from "sonner";
22+
import {getErrorMessage} from "@/lib/error-utils";
2223

2324
interface MessageInputProps {
2425
onSendMessage: (message: string, type: "user" | "raw") => void;
@@ -80,10 +81,9 @@ export default function MessageInput({
8081
if (response.ok) {
8182
setMessage(oldMessage => oldMessage + ' @"' + response.filePath + '"');
8283
}
83-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
84-
} catch (error: any) {
84+
} catch (error) {
8585
toast.error("Failed to and upload file:", {
86-
description: error.message,
86+
description: getErrorMessage(error),
8787
});
8888
}
8989
}

0 commit comments

Comments
 (0)