From 136dc2eb1254e3f944caa1317e128283d97aa3a5 Mon Sep 17 00:00:00 2001 From: Igor Loskutov Date: Mon, 16 Mar 2026 07:33:06 -0400 Subject: [PATCH] server: return proper HTTP status codes for error responses (#3707) Several error paths in the /inference and /load endpoints returned HTTP 200 with a JSON error body, making it impossible for clients to distinguish errors from successful responses by status code. Set 400 for client errors (missing file field, unreadable audio, missing/invalid model) and 500 for server errors (ffmpeg conversion failure). The two existing status-code sites (499 for client disconnect, 500 for processing failure) are unchanged. --- examples/server/server.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/server/server.cpp b/examples/server/server.cpp index c5354efc..8ace43bf 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -811,6 +811,7 @@ int main(int argc, char ** argv) { { fprintf(stderr, "error: no 'file' field in the request\n"); const std::string error_resp = "{\"error\":\"no 'file' field in the request\"}"; + res.status = 400; res.set_content(error_resp, "application/json"); return; } @@ -837,6 +838,7 @@ int main(int argc, char ** argv) { std::string error_resp = "{\"error\":\"Failed to execute ffmpeg command.\"}"; const bool is_converted = convert_to_wav(temp_filename, error_resp); if (!is_converted) { + res.status = 500; res.set_content(error_resp, "application/json"); return; } @@ -846,6 +848,7 @@ int main(int argc, char ** argv) { { fprintf(stderr, "error: failed to read WAV file '%s'\n", temp_filename.c_str()); const std::string error_resp = "{\"error\":\"failed to read WAV file\"}"; + res.status = 400; res.set_content(error_resp, "application/json"); std::remove(temp_filename.c_str()); return; @@ -857,6 +860,7 @@ int main(int argc, char ** argv) { { fprintf(stderr, "error: failed to read audio data\n"); const std::string error_resp = "{\"error\":\"failed to read audio data\"}"; + res.status = 400; res.set_content(error_resp, "application/json"); return; } @@ -1127,6 +1131,7 @@ int main(int argc, char ** argv) { { fprintf(stderr, "error: no 'model' field in the request\n"); const std::string error_resp = "{\"error\":\"no 'model' field in the request\"}"; + res.status = 400; res.set_content(error_resp, "application/json"); return; } @@ -1135,6 +1140,7 @@ int main(int argc, char ** argv) { { fprintf(stderr, "error: 'model': %s not found!\n", model.c_str()); const std::string error_resp = "{\"error\":\"model not found!\"}"; + res.status = 400; res.set_content(error_resp, "application/json"); return; }