Prevent crashes when failing to decode event json

This commit is contained in:
William Brawner 2023-09-26 20:25:13 -06:00
parent 92495c06c4
commit 68409a7484
Signed by: wbrawner
GPG key ID: 8FF12381C6C90D35
2 changed files with 10 additions and 1 deletions

View file

@ -39,7 +39,11 @@ internal data class Event(
val props: String?
) {
companion object {
fun fromJson(json: String): Event = Json.decodeFromString(json)
fun fromJson(json: String): Event? = try {
Json.decodeFromString(json)
} catch (ignored: Exception) {
null
}
}
}

View file

@ -95,6 +95,11 @@ internal class NetworkFirstPlausibleClient(
config.eventDir.mkdirs()
config.eventDir.listFiles()?.forEach {
val event = Event.fromJson(it.readText())
if (event == null) {
Timber.tag("Plausible").e("Failed to decode event JSON, discarding")
it.delete()
return@forEach
}
try {
postEvent(event)
} catch (e: IOException) {