From 58c5e95879f8759c7b3ef7438eaf72d566bede84 Mon Sep 17 00:00:00 2001 From: Jacob Lasky Date: Sat, 25 Jul 2026 13:24:56 -0400 Subject: [PATCH] fix(errors): stop repeating err_code that is already in err_msg Observed in the live response after the previous change: Deepgram 400: Bad Request: Nova-3 models do not support more than one alternative. (Bad Request) Deepgram usually prefixes err_msg with err_code, so appending it unconditionally stutters. Append only when the code adds information. --- app.py | 7 ++++++- tests/test_wire_contract.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 5390f18..0fd0c6c 100644 --- a/app.py +++ b/app.py @@ -346,7 +346,12 @@ def _clean_error(e: Exception) -> str: detail = body.get("err_msg") or body.get("error") or body.get("message") code = body.get("err_code") if detail: - return f"Deepgram {status}: {detail}" + (f" ({code})" if code else "") + # Deepgram often prefixes err_msg with err_code already + # ("Bad Request: Nova-3 models do not support..."), so appending + # it unconditionally reads as "... (Bad Request)" twice. + if code and str(code).lower() not in str(detail).lower(): + return f"Deepgram {status}: {detail} ({code})" + return f"Deepgram {status}: {detail}" return f"Deepgram {status}: {json_mod.dumps(body)[:500]}" except ValueError: text = (e.response.text or "").strip() diff --git a/tests/test_wire_contract.py b/tests/test_wire_contract.py index d799a05..26c6e3c 100644 --- a/tests/test_wire_contract.py +++ b/tests/test_wire_contract.py @@ -230,3 +230,37 @@ def test_clean_error_still_strips_sdk_request_headers(): msg = app_mod._clean_error(e) assert "sk-secret" not in msg assert "Invalid credentials" in msg + + +def test_clean_error_does_not_repeat_the_error_code(): + """Deepgram usually prefixes err_msg with err_code, so appending it + unconditionally produced "Bad Request: ... (Bad Request)". Observed live.""" + import httpx + + import app as app_mod + + request = httpx.Request("POST", "https://api.deepgram.com/v1/listen") + response = httpx.Response( + 400, + json={ + "err_code": "Bad Request", + "err_msg": "Bad Request: Nova-3 models do not support more than one alternative.", + }, + request=request, + ) + msg = app_mod._clean_error( + httpx.HTTPStatusError("boom", request=request, response=response) + ) + assert msg == ( + "Deepgram 400: Bad Request: Nova-3 models do not support more than one alternative." + ) + + # A code that adds information is still appended. + response = httpx.Response( + 400, json={"err_code": "INVALID_PARAM", "err_msg": "something went wrong"}, + request=request, + ) + msg = app_mod._clean_error( + httpx.HTTPStatusError("boom", request=request, response=response) + ) + assert msg == "Deepgram 400: something went wrong (INVALID_PARAM)"