refactoring of error handler