Skip to content

Commit 63e304d

Browse files
committed
chat: show an error alert when send message fails
1 parent f1c4f5b commit 63e304d

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

chat/src/components/ChatInterface.tsx

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,16 @@ export default function ChatInterface() {
109109
};
110110
}, [AgentAPIUrl]);
111111

112+
const [error, setError] = useState<string | null>(null);
113+
112114
// Send a new message
113115
const sendMessage = async (content: string, type: 'user' | 'raw' = 'user') => {
114116
// For user messages, require non-empty content
115117
if (type === 'user' && !content.trim()) return;
116118

119+
// Clear any previous errors
120+
setError(null);
121+
117122
// For raw messages, don't set loading state as it's usually fast
118123
if (type === 'user') {
119124
setLoading(true);
@@ -126,16 +131,35 @@ export default function ChatInterface() {
126131
'Content-Type': 'application/json',
127132
},
128133
body: JSON.stringify({
129-
content,
134+
content: content,
130135
type
131136
}),
132137
});
133138

134139
if (!response.ok) {
135-
console.error('Failed to send message:', await response.json());
140+
const errorData = await response.json();
141+
console.error('Failed to send message:', errorData);
142+
const detail = errorData.detail;
143+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
144+
const messages = "errors" in errorData ? errorData.errors.map((e: any) => e.message).join(", ") : "";
145+
146+
const fullDetail = `${detail}: ${messages}`;
147+
setError(`Failed to send message: ${fullDetail}`);
148+
// Auto-clear error after 5 seconds
149+
setTimeout(() => setError(null), 5000);
136150
}
137-
} catch (error) {
151+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
152+
} catch (error: any) {
138153
console.error('Error sending message:', error);
154+
const detail = error.detail;
155+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
156+
const messages = "errors" in error ? error.errors.map((e: any) => e.message).join("\n") : "";
157+
158+
const fullDetail = `${detail}: ${messages}`;
159+
160+
setError(`Error sending message: ${fullDetail}`);
161+
// Auto-clear error after 5 seconds
162+
setTimeout(() => setError(null), 5000);
139163
} finally {
140164
if (type === 'user') {
141165
setLoading(false);
@@ -154,6 +178,18 @@ export default function ChatInterface() {
154178
</span>
155179
</div>
156180

181+
{error && (
182+
<div className="bg-red-100 border border-red-400 text-red-700 px-4 py-2 text-sm relative">
183+
<span className="block sm:inline">{error}</span>
184+
<button
185+
onClick={() => setError(null)}
186+
className="absolute top-0 bottom-0 right-0 px-4 py-2"
187+
>
188+
×
189+
</button>
190+
</div>
191+
)}
192+
157193
<MessageList messages={messages} />
158194

159195
<MessageInput onSendMessage={sendMessage} disabled={loading} />

0 commit comments

Comments
 (0)