Is there a way to override the default http responses?

For example when I try to hit the /login endpoint with an invalid username or password, I get a broken pipe Werkzeug Debugger html page and a 500 status code internal server code as the response, is there a way for me to hook into the response request cycle and be able to modify the response for say, a SessionExpired or Authentication Error to return to proper status code and some json data instead?