Introduced standardized error response formats for API errors, including middleware for consistent request IDs and exception handlers. Updated the frontend to parse and process these error responses, and documented the error format in the README for reference.
32 lines
777 B
Python
32 lines
777 B
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
def error_payload(code: str, message: str, details: Any, request_id: str) -> dict[str, Any]:
|
|
return {
|
|
"code": code,
|
|
"message": message,
|
|
"details": details,
|
|
"request_id": request_id,
|
|
}
|
|
|
|
|
|
def http_status_to_code(status_code: int) -> str:
|
|
mapping = {
|
|
400: "bad_request",
|
|
401: "unauthorized",
|
|
403: "forbidden",
|
|
404: "not_found",
|
|
405: "method_not_allowed",
|
|
409: "conflict",
|
|
422: "validation_error",
|
|
429: "rate_limited",
|
|
500: "internal_error",
|
|
502: "bad_gateway",
|
|
503: "service_unavailable",
|
|
504: "gateway_timeout",
|
|
}
|
|
return mapping.get(status_code, f"http_{status_code}")
|
|
|