Fix error handling to return sensible HTTP error codes.