Extract error handling into its own method