Refactored the handleError method.