Separate HTTP 500 error handling

Separate out the error handling for 500 errors. Previously the code assumed
that all exceptions in the error handler were a subclass of HTTPError. This is
*not* the case if one handles 500s, so they must be treated separately.

This commit also refactors 401 handling back into the main HTTP error handler.