Factor out common error handling.