refactor code for  register error handler