Fixed #22058, #21668 -- Refactored handlers and added a general mechanism to deal with client and server errors with better messages.