refactor error handling mechanism