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.
This commit is contained in:
Jack Michaud
2023-01-02 19:01:29 -05:00
parent 329a3bfd7a
commit 5bb9a397b4
2 changed files with 14 additions and 1 deletions
+6 -1
View File
@@ -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(),
)
@@ -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