Skip to content

fix: return 404 for not-found session in ideas_api.py cancel endpoint - #86

Open
hazelp343 wants to merge 1 commit into
OpenNSWM-Lab:mainfrom
hazelp343:fix/ideas-cancel-status-code
Open

fix: return 404 for not-found session in ideas_api.py cancel endpoint#86
hazelp343 wants to merge 1 commit into
OpenNSWM-Lab:mainfrom
hazelp343:fix/ideas-cancel-status-code

Conversation

@hazelp343

Copy link
Copy Markdown

Background

The POST /sessions/{session_id}/cancel endpoint in backend/app/modules/idea/ideas_api.py catches ValueError from the service layer and returns 400 Bad Request for all error cases. However, the service distinguishes between two semantically different errors:

  • Session not found (should be 404)
  • Session in terminal state (should be 409 Conflict)

Returning 400 for both conflates "invalid request" with "resource state conflict", making it harder for API clients to handle errors correctly.

Changes

Modified the exception handler in cancel_session to inspect the error message and return the appropriate HTTP status code:

  • If the message contains "not found", return 404 Not Found
  • Otherwise (terminal state), return 409 Conflict

The service layer at backend/app/modules/idea/service.py:3337 raises:

  • ValueError("Session {id} not found") for missing sessions
  • ValueError("Cannot cancel session in {status} state") for terminal sessions

Verification

  • Reviewed the diff to confirm the conditional logic and status code mapping
  • Verified the service layer error messages match the detection pattern
  • The change is backwards compatible in the sense that clients checking for non-200 responses will still work; clients specifically handling 404/409 will now get correct semantics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant