[fix] put the error handler into its own function