Improve error handling of failed tasks