From 5bb9a397b494fc0cc7232e0dc852d9b674291e87 Mon Sep 17 00:00:00 2001 From: Jack Michaud Date: Mon, 2 Jan 2023 19:01:29 -0500 Subject: [PATCH] feat: add OasstErrorResponse to protocols Using a shared protocol to serialize the error in the backend allows clients to use that same protocol to deserialize it. Changes to this protocol will be caught in tests. --- backend/main.py | 7 ++++++- oasst-shared/oasst_shared/schemas/protocol.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/backend/main.py b/backend/main.py index e6b3bdce..cb682a9f 100644 --- a/backend/main.py +++ b/backend/main.py @@ -26,8 +26,13 @@ app = fastapi.FastAPI(title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V @app.exception_handler(OasstError) async def oasst_exception_handler(request: fastapi.Request, ex: OasstError): logger.error(f"{request.method} {request.url} failed: {repr(ex)}") + return fastapi.responses.JSONResponse( - status_code=int(ex.http_status_code), content={"message": ex.message, "error_code": ex.error_code} + status_code=int(ex.http_status_code), + content=protocol_schema.OasstErrorResponse( + message=ex.message, + error_code=OasstErrorCode(ex.error_code), + ).dict(), ) diff --git a/oasst-shared/oasst_shared/schemas/protocol.py b/oasst-shared/oasst_shared/schemas/protocol.py index 652a2c78..83375d8f 100644 --- a/oasst-shared/oasst_shared/schemas/protocol.py +++ b/oasst-shared/oasst_shared/schemas/protocol.py @@ -4,6 +4,7 @@ from typing import List, Literal, Optional, Union from uuid import UUID, uuid4 import pydantic +from oasst_shared.exceptions import OasstErrorCode from pydantic import BaseModel, Field @@ -293,3 +294,10 @@ class UserScore(BaseModel): class LeaderboardStats(BaseModel): leaderboard: List[UserScore] + + +class OasstErrorResponse(BaseModel): + """The format of an error response from the OASST API.""" + + error_code: OasstErrorCode + message: str