Correctly send the error to the client in proper formatted json with the right status code.