Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return null with pathConfiguredFailedToParse exception #91

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ internal class PathConfigurationLoader(val context: Context) : CoroutineScope {
repository.cacheConfigurationForUrl(context, url, pathConfiguration)
}

private fun load(json: String) = try {
json.toObject(object : TypeToken<PathConfiguration>() {})
} catch(e: Exception) {
logError("pathConfiguredFailedToParse", e)
null
fun load(json: String): PathConfiguration? {
return try {
json.toObject(object : TypeToken<PathConfiguration>() {})
} catch (e: Exception) {
logError("pathConfiguredFailedToParse", e)
null
}
Comment on lines -70 to +72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually change any logic here. In Kotlin, = try is just shorthand for return try. Looking at this closer, we're already catching parsing exceptions correctly, just with a slightly different implementation from turbo-android.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I noticed when I started this. I meant to look into it further, but I became busy with work.

Thanks for coming back to it.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ class PathConfigurationRepositoryTest : BaseRepositoryTest() {
assertThat(config?.rules?.size).isEqualTo(10)
}

@Test
fun getMalformedRemoteConfiguration() {
enqueueResponse("malformed-body.json")
val loader = PathConfigurationLoader(context)

runBlocking {
launch(Dispatchers.Main) {
val json = repository.getRemoteConfiguration(baseUrl())
val pathConfiguration = json?.let { loader.load(it) }
assertThat(pathConfiguration).isNull()
}
}
}
Comment on lines +56 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more appropriate place for this test is in PathConfigurationTest, since parsing exceptions are caught at the PathConfigurationLoader layer, not the repository layer.


@Test
fun getCachedConfiguration() {
val url = "https://turbo.hotwired.dev/demo/configurations/android-v1.json"
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/resources/http-responses/malformed-body.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
malformed-body