https://kotlinlang.org logo
#rx
Title
# rx
u

ursus

06/21/2019, 3:25 PM
Copy code
private fun doPostMessage(...) {
    var messageId: Long? = null
    try {
		messageId = messageRepository.insertSendingTextMessage(...)

        val apiMessage = apiClient.postMessage(...)

        messageRepository.updateSentMessageAndParents(apiMessage)

    } catch (ex: Exception) {
        if (messageId != null) messageRepository.updateMessageState(messageId, MessageState.ERROR)
        throw ex
    }
}
k

kioba

06/21/2019, 3:31 PM
if
messageRepository.insertSendingTextMessage(...)
fails you still crashing the app. without properly calling the
updateMessageState
u

ursus

06/21/2019, 3:51 PM
Why? I'd catch it, update the updateMessageState with ERROR and rethrow as a signal downstream it failed
k

kioba

06/21/2019, 3:53 PM
if
insertSendingTextMessage
fails you won’t set the
messageId
so
updateMessageState
will be called with an uninitialised variable
u

ursus

06/21/2019, 3:53 PM
oh, yea, in that case the update is skipped, since messageId will be null, its pseudo code, sorry
👍 1