Skip to content

Commit 380489d

Browse files
committed
improve error handling
1 parent b4ed831 commit 380489d

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

llms/extensions/app/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ async def run_chat(chat_req, context_req):
223223
await ctx.chat_completion(chat_req, context=context_req)
224224
except Exception as ex:
225225
ctx.err("run_chat", ex)
226-
# not necessary to update thread in db with error as it's done in chat_error filter
226+
# shouldn't be necessary to update thread in db with error as it's done in chat_error filter
227+
thread = thread_dto(g_db.get_thread(id, user=ctx.get_username(request)))
228+
if thread and not thread.get("error"):
229+
await chat_error(ex, context)
227230

228231
asyncio.create_task(run_chat(chat, context))
229232

llms/main.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -874,8 +874,18 @@ def save_image_to_cache(base64_data, filename, image_info, ignore_info=False):
874874
async def response_json(response):
875875
text = await response.text()
876876
if response.status >= 400:
877-
_dbg(f"HTTP {response.status} {response.reason}: {text}")
878-
raise HTTPError(response.status, reason=response.reason, body=text, headers=dict(response.headers))
877+
message = "HTTP " + str(response.status) + " " + response.reason
878+
_dbg(f"HTTP {response.status} {response.reason}\n{dict(response.headers)}\n{text}")
879+
try:
880+
body = json.loads(text)
881+
if "message" in body:
882+
message = body["message"]
883+
elif "error" in body:
884+
message = body["error"]
885+
except Exception:
886+
if text:
887+
message += ": " + text[:100]
888+
raise Exception(message)
879889
response.raise_for_status()
880890
body = json.loads(text)
881891
return body
@@ -1946,12 +1956,11 @@ async def g_chat_completion(chat, context=None):
19461956
first_exception = e
19471957
context["stackTrace"] = traceback.format_exc()
19481958
_err(f"Provider {provider_name} failed", first_exception)
1949-
await g_app.on_chat_error(e, context)
1950-
19511959
continue
19521960

19531961
# If we get here, all providers failed
19541962
if first_exception:
1963+
await g_app.on_chat_error(first_exception, context or {"chat": chat})
19551964
raise first_exception
19561965

19571966
e = Exception("All providers failed")
@@ -3871,9 +3880,9 @@ async def update_extensions(extension_name):
38713880
asyncio.run(update_extensions(cli_args.update))
38723881
return ExitCode.SUCCESS
38733882

3883+
g_app.add_allowed_directory(tempfile.gettempdir()) # add temp directory
38743884
g_app.add_allowed_directory(home_llms_path(".agent")) # info for agents, e.g: skills
38753885
g_app.add_allowed_directory(os.getcwd()) # add current directory
3876-
g_app.add_allowed_directory(tempfile.gettempdir()) # add temp directory
38773886

38783887
g_app.extensions = install_extensions()
38793888

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)