Skip to content

Commit

Permalink
Add list class method to JobStatus Enum. More informative error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelltaylor committed Apr 18, 2024
1 parent f9978f5 commit 6dd03e2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
9 changes: 9 additions & 0 deletions florist/api/db/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class JobStatus(Enum):
FINISHED_WITH_ERROR = "FINISHED_WITH_ERROR"
FINISHED_SUCCESSFULLY = "FINISHED_SUCCESSFULLY"

@classmethod
def list(cls) -> List[str]:
"""
List all the supported clients.
:return: (List[str]) a list of valid job statuses.
"""
return [status.value for status in JobStatus]


class ClientInfo(BaseModel):
"""Define the information of an FL client."""
Expand Down
4 changes: 2 additions & 2 deletions florist/api/routes/server/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ async def list_jobs_with_status(status: JobStatus, request: Request) -> List[Dic
status = jsonable_encoder(status)

assert isinstance(status, str)
if status not in JobStatus._member_names_:
msg = f"status is not valid. status: {status}."
if status not in JobStatus.list():
msg = f"status {status} is not valid. Valid statuses: {JobStatus.list()}"
raise HTTPException(status_code=400, detail=msg)

job_db = request.app.database[JOB_DATABASE_NAME]
Expand Down
3 changes: 2 additions & 1 deletion florist/tests/integration/api/routes/server/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,5 @@ async def test_list_jobs_with_invalid_status(mock_request) -> None:
await list_jobs_with_status("NON_EXISTENT_STATUS", mock_request)

assert exception_info.value.status_code == 400
assert "status is not valid. status: NON_EXISTENT_STATUS" in exception_info.value.detail
exc_str = f"status NON_EXISTENT_STATUS is not valid. Valid statuses: {JobStatus.list()}"
assert exc_str in exception_info.value.detail

0 comments on commit 6dd03e2

Please sign in to comment.