fix(worker): return structured internal endpoint errors

This commit is contained in:
2026-09-20 10:40:13 +03:30
parent b51b7d5954
commit 6ce567646b
2 changed files with 66 additions and 6 deletions

View File

@@ -14,11 +14,36 @@ class WorkerHTTPRequestHandler(BaseHTTPRequestHandler):
"""
Lightweight HTTP request handler for worker internal metrics and stats.
"""
error_content_type = "application/json"
def log_message(self, format: str, *args) -> None:
# Suppress default noisy stderr logging
pass
def send_error(self, code: int, message: Optional[str] = None, explain: Optional[str] = None) -> None:
from http import HTTPStatus
try:
if hasattr(self, "headers") and self.headers:
content_length = int(self.headers.get("Content-Length", 0))
if content_length > 0:
self.rfile.read(min(content_length, 65536))
except Exception:
pass
try:
status = HTTPStatus(code)
short_msg = status.phrase
except ValueError:
short_msg = "error"
err_msg = message or short_msg
detail = explain or err_msg
body = json.dumps({"error": err_msg, "detail": detail}).encode("utf-8")
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.send_header("Connection", "close")
self.end_headers()
self.wfile.write(body)
def do_GET(self) -> None:
if self.path == "/internal/stats":
stats = worker_metrics.get_internal_stats()
@@ -51,12 +76,7 @@ class WorkerHTTPRequestHandler(BaseHTTPRequestHandler):
self.wfile.write(body)
return
self.send_response(404)
self.send_header("Content-Type", "application/json")
body = b'{"error": "not_found"}'
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
self.send_error(404, "not_found", f"Cannot GET {self.path}")
class WorkerInternalServer: