I usually see HTTP status codes being checked as integer numbers:
if ((status >= 200) && (status < 300)) // or `<= 299`
This is rather verbose. But if you think about it, it's the first digit in the number that has a special value, and there are only five of them:
- 1xx: server programmer is too smart
- 2xx: success
- 3xx: your client HTTP library is too dumb
- 4xx: you screwed up
- 5xx: server screwed up
When treated as strings, checking for the error class looks a bit better:
if (status == '2')
Unfortunately, the ensuing party is pre-pooped by most client HTTP libraries helpfully casting the status to
Dear HTTP client libraries! Consider adding
response.error_class to your responses.
Probably not much better, but still:
Nitpick: why is 451 "you screwed up"? It's more like "your government screwed up".
When treated as ints, checking for the error class looks a bit better:
451 is up to the user to fix, not the server...
About 451. Maybe it's worth thinking about this like "you let your government do this stupid shit, so you screwed up".