Factorize error handling