Return a JSON response when handling errors if it is requested