Don't crash on unexpected deserialization error

This commit is contained in:
nielsandriesse 2021-05-12 16:21:53 +10:00
parent bb850cf99e
commit c5e0589751
2 changed files with 17 additions and 5 deletions

View File

@ -22,6 +22,6 @@ interface Job {
interface Factory<T : Job> { interface Factory<T : Job> {
fun create(data: Data): T fun create(data: Data): T?
} }
} }

View File

@ -104,17 +104,29 @@ class MessageSendJob(val message: Message, val destination: Destination) : Job {
class Factory : Job.Factory<MessageSendJob> { class Factory : Job.Factory<MessageSendJob> {
override fun create(data: Data): MessageSendJob { override fun create(data: Data): MessageSendJob? {
val serializedMessage = data.getByteArray(MESSAGE_KEY) val serializedMessage = data.getByteArray(MESSAGE_KEY)
val serializedDestination = data.getByteArray(DESTINATION_KEY) val serializedDestination = data.getByteArray(DESTINATION_KEY)
val kryo = Kryo() val kryo = Kryo()
// Message // Message
val messageInput = Input(serializedMessage) val messageInput = Input(serializedMessage)
val message = kryo.readClassAndObject(messageInput) as Message val message: Message
try {
message = kryo.readClassAndObject(messageInput) as Message
} catch (e: Exception) {
Log.e("Loki", "Couldn't deserialize message send job.", e)
return null
}
messageInput.close() messageInput.close()
// Destination // Destination
val destinationInput = Input(serializedDestination) val destinationInput = Input(serializedDestination)
val destination = kryo.readClassAndObject(destinationInput) as Destination val destination: Destination
try {
destination = kryo.readClassAndObject(destinationInput) as Destination
} catch (e: Exception) {
Log.e("Loki", "Couldn't deserialize message send job.", e)
return null
}
destinationInput.close() destinationInput.close()
// Return // Return
return MessageSendJob(message, destination) return MessageSendJob(message, destination)