Non-happy path – Unexpected crash

Let's take a look at how we are waiting for each deferred to complete:

private fun asyncLoadNews() = launch {
val requests = mutableListOf<Deferred<List<String>>>()

feeds.mapTo(requests) {
asyncFetchHeadlines(it, dispatcher)
}

requests.forEach {
it.await()
}

...
}

Since we are using await() to wait for the completion of our coroutines, any exception happening inside them will be propagated to the current thread. This means that there are two scenarios in which the application will crash easily:

  • There's no internet connection
  • The URL of one or more feeds is invalid or incorrect

Let's test one of those out. We can add an invalid URL to the list of feeds, which is something like this:

private val feeds = listOf(
"https://www.npr.org/rss/rss.php?id=1001",
"http://rss.cnn.com/rss/cnn_topstories.rss",
"http://feeds.foxnews.com/foxnews/politics?format=xml",
"htt:myNewsFeed"
)

If we run the application, it will crash as soon as it tries to fetch this feed:

Also, the log will be explicit about the exception. Since the protocol doesn't exist, the request fails:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.196.244