how to emit error event and handle error inside error handler