Cleaned up generation of http errors to deliver the correct message and error type.