Skip to content

Commit

Permalink
android notif: Parse zulip_message_id at crunchy shell.
Browse files Browse the repository at this point in the history
This is a typical example of the "crunchy shell" pattern at work.
Previously, if for some reason we received an FCM message with no
`zulip_message_id`, or one that didn't parse as a decimal integer,
the app would simply crash.

Now we'll instead ignore that FCM message, and log a warning to the
system log for possible use in debugging.
  • Loading branch information
gnprice committed Mar 7, 2019
1 parent 0f7d055 commit 58e074b
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal class MessageFcmMessage private constructor(
val topic: String?,
val pmUsers: String?,

val zulipMessageId: Int,
val content: String,
val time: String,

Expand All @@ -45,9 +46,6 @@ internal class MessageFcmMessage private constructor(
val baseURL: String?
get() = bundle.getString("base_url")

val zulipMessageId: Int
get() = Integer.parseInt(bundle.getString("zulip_message_id"))

protected fun copy(): MessageFcmMessage {
return fromBundle(bundle)
}
Expand Down Expand Up @@ -77,8 +75,10 @@ internal class MessageFcmMessage private constructor(
topic = bundle.getString("topic"),
pmUsers = bundle.getString("pm_users"),

zulipMessageId = bundle.requireIntString("zulip_message_id"),
content = bundle.requireString("content"),
time = bundle.requireString("time"),

bundle = bundle.clone() as Bundle
)
}
Expand All @@ -89,4 +89,13 @@ private fun Bundle.requireString(key: String): String {
return getString(key) ?: throw FcmMessageParseException("missing expected field: $key")
}

private fun Bundle.requireIntString(key: String): Int {
val s = requireString(key);
return try {
Integer.parseInt(s)
} catch (e: NumberFormatException) {
throw FcmMessageParseException("invalid format where int expected: $key -> $s")
}
}

class FcmMessageParseException(errorMessage: String): RuntimeException(errorMessage)

0 comments on commit 58e074b

Please sign in to comment.