Make error handling more generic