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:

When treated as strings, checking for the error class looks a bit better:

if (status[0] == '2')

Unfortunately, the ensuing party is pre-pooped by most client HTTP libraries helpfully casting the status to int.

Dear HTTP client libraries! Consider adding response.error_class to your responses.

Comments: 4 (feed)

  1. Alexey

    Probably not much better, but still:

    if status // 100 == 2:
    

    In JS:

    if (Math.floor(status / 100) == 2)
    

    Nitpick: why is 451 "you screwed up"? It's more like "your government screwed up".

  2. Sergey

    When treated as ints, checking for the error class looks a bit better:

    if (status / 100 == 2)
    
  3. jyax

    451 is up to the user to fix, not the server...

  4. Eugene

    About 451. Maybe it's worth thinking about this like "you let your government do this stupid shit, so you screwed up".

Add comment

Format with markdown