refactor the error handler

calling next outside the stack could mess up an app