refactor common error handler