Card 04/ 07

ComparisonDifficulty: Intermediate1 min

Which Exception, Which Status

A status code is a claim about what happened, and the exception that triggered it should match that claim — not just be whatever was easiest to throw at the time.

Common failures and the status that fits
What happenedStatusCustom exception example
The resource doesn't exist404 Not FoundRideNotFoundException
The request itself is malformed or fails validation400 Bad RequestMethodArgumentNotValidException (built in)
The caller isn't authenticated401 UnauthorizedAuthenticationException-family
The resource exists but conflicts with the request409 ConflictDuplicateRideException

The rule that decides each row: 404 means the lookup failed, 400 means the request was never valid to begin with, and 409 means the request was valid but collided with something that already exists. A caller reading the status code alone should be able to tell which of these three happened.