Skip to content

Commit a5ec04b

Browse files
user friendly errors
1 parent c8f3e4e commit a5ec04b

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

api/api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import json
3+
import logging
34

45
from dotenv import dotenv_values
56
import torch
@@ -27,6 +28,8 @@
2728
else:
2829
TOKEN = dotenv_values()["HF_TOKEN"]
2930

31+
logger = logging.getLogger(__name__)
32+
3033
ml_models = {}
3134

3235

@@ -81,7 +84,7 @@ async def generate(
8184
"""
8285
Generate an output stream based on a prompt, using a LLM (Llama 3.2 1B Instruct).
8386
"""
84-
print(safenudge)
87+
logger.info("generate called: safenudge=%s", safenudge)
8588
if not safenudge:
8689
data = generate_output_stream(
8790
init_prompt=init_prompt,

client/src/api/stream.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@ export async function* streamEndpoint(url, params, signal) {
3838
});
3939

4040
if (!res.ok) {
41-
const text = await res.text().catch(() => "");
42-
throw new Error(`Request to ${url} failed: ${res.status} ${text}`);
41+
const friendlyMessages = {
42+
429: "Too many requests — please wait a moment before trying again.",
43+
422: "Invalid request parameters.",
44+
500: "Server error. Please try again.",
45+
502: "Server is unavailable. Please try again shortly.",
46+
503: "Server is unavailable. Please try again shortly.",
47+
};
48+
const msg = friendlyMessages[res.status] ?? `Request failed (${res.status}).`;
49+
throw new Error(msg);
4350
}
4451
if (!res.body) {
4552
throw new Error("Streaming response body unavailable");

0 commit comments

Comments
 (0)