Skip to content

Commit

Permalink
Merge pull request #53 from tw-mosip/add-exception-handling-to-androi…
Browse files Browse the repository at this point in the history
…d-missing-files-hotfix

[Android] Add exception handling to missing Classes
  • Loading branch information
krishnakumar4a4 authored Apr 19, 2023
2 parents c6a2231 + 73d989a commit 474e1ab
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.util.Log
import io.mosip.tuvali.ble.central.impl.Controller
import io.mosip.tuvali.ble.central.ICentralListener
import io.mosip.tuvali.ble.central.state.message.*
import io.mosip.tuvali.openid4vpble.exception.exception.StateHandlerException
import io.mosip.tuvali.openid4vpble.exception.StateHandlerException
import io.mosip.tuvali.transfer.Util.Companion.getLogTag

class StateHandler(
Expand Down Expand Up @@ -257,7 +257,7 @@ class StateHandler(
super.dispatchMessage(msg)
} catch (e: Exception) {
listener.onException(StateHandlerException("Exception in Central State Handler", e))
Log.d(logTag, "dispatchMessage " + e.message)
Log.e(logTag, "dispatchMessage " + e.message)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.util.Log
import io.mosip.tuvali.ble.peripheral.state.IMessageSender
import io.mosip.tuvali.ble.peripheral.state.message.*
import io.mosip.tuvali.transfer.Util.Companion.getLogTag
import java.lang.Exception

const val MTU_HEADER_SIZE = 3

class Controller(val context: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.util.Log
import com.facebook.common.util.Hex
import io.mosip.tuvali.ble.peripheral.IPeripheralListener
import io.mosip.tuvali.ble.peripheral.impl.Controller
import io.mosip.tuvali.openid4vpble.exception.exception.StateHandlerException
import io.mosip.tuvali.openid4vpble.exception.StateHandlerException
import io.mosip.tuvali.ble.peripheral.state.message.*
import io.mosip.tuvali.transfer.Util.Companion.getLogTag

Expand Down Expand Up @@ -158,7 +158,7 @@ class StateHandler(
super.dispatchMessage(msg)
} catch (e: Exception) {
peripheralListener.onException(StateHandlerException("Exception in Peripheral State Handler", e))
Log.d(logTag, "dispatchMessage " + e.message)
Log.e(logTag, "dispatchMessage " + e.message)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
return tryExecuteSync.run {
if (verifier == null) {
Log.d(logTag, "synchronized getConnectionParameters new verifier object at ${System.nanoTime()}")
verifier = Verifier(reactContext, this::emitNearbyMessage, this::emitNearbyEvent, this::onException)
verifier = Verifier(reactContext, this::emitNearbyMessage, this::emitNearbyEvent, bleExceptionHandler::handleException)
verifier?.generateKeyPair()
}

Expand All @@ -62,15 +62,6 @@ class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
}.orEmpty()
}

private fun onException(exception: Exception){
if(exception.cause != null){
Log.e(logTag, "Exception: ${exception.message}");
bleExceptionHandler.handleException(exception.cause!!)
} else {
bleExceptionHandler.handleException(exception)
}
}

@ReactMethod(isBlockingSynchronousMethod = true)
fun getConnectionParametersDebug(): String {
return getConnectionParameters()
Expand All @@ -83,7 +74,7 @@ class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
tryExecuteSync.run {
if (wallet == null) {
Log.d(logTag, "synchronized setConnectionParameters new wallet object at ${System.nanoTime()}")
wallet = Wallet(reactContext, this::emitNearbyMessage, this::emitNearbyEvent, this::onException)
wallet = Wallet(reactContext, this::emitNearbyMessage, this::emitNearbyEvent, bleExceptionHandler::handleException)
}
val paramsObj = JSONObject(params)
val firstPartOfPk = paramsObj.getString("pk")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.mosip.tuvali.openid4vpble.exception

import android.util.Log
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.CallbackImpl
import io.mosip.tuvali.transfer.Util
import io.mosip.tuvali.verifier.exception.VerifierException
import io.mosip.tuvali.verifier.exception.VerifierExceptionHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.mosip.tuvali.openid4vpble.exception.exception
package io.mosip.tuvali.openid4vpble.exception

class StateHandlerException(message: String, cause: Throwable): Exception(message, cause) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.mosip.tuvali.openid4vpble.exception.exception
package io.mosip.tuvali.openid4vpble.exception

class TransferHandlerException(message: String, cause: Throwable): Exception(message, cause) {
}
7 changes: 4 additions & 3 deletions android/src/main/java/io/mosip/tuvali/verifier/Verifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.mosip.tuvali.transfer.TransferReportRequest
import io.mosip.tuvali.transfer.Util
import io.mosip.tuvali.transfer.Util.Companion.getLogTag
import io.mosip.tuvali.verifier.exception.UnsupportedMTUSizeException
import io.mosip.tuvali.verifier.exception.VerifierException
import io.mosip.tuvali.verifier.transfer.ITransferListener
import io.mosip.tuvali.verifier.transfer.TransferHandler
import io.mosip.tuvali.verifier.transfer.message.InitTransferMessage
Expand All @@ -31,7 +32,7 @@ class Verifier(
context: Context,
private val messageResponseListener: (String, String) -> Unit,
private val eventResponseListener: (String) -> Unit,
private val onBLEException: (Exception) -> Unit
private val handleException: (Exception) -> Unit
) :
IPeripheralListener, ITransferListener {
private var secretsTranslator: SecretsTranslator? = null;
Expand Down Expand Up @@ -200,8 +201,8 @@ class Verifier(
}
}

override fun onException(e: Exception) {
onBLEException(e)
override fun onException(exception: Exception) {
handleException(VerifierException("Exception in Verifier", exception))
}

override fun onClosed() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package io.mosip.tuvali.verifier.exception

open class VerifierException(message: String): Exception(message)
open class VerifierException(message: String, cause: Throwable? = null): Exception(message, cause)
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package io.mosip.tuvali.verifier.exception

import android.util.Log
import io.mosip.tuvali.openid4vpble.exception.StateHandlerException
import io.mosip.tuvali.openid4vpble.exception.TransferHandlerException
import io.mosip.tuvali.transfer.Util

class VerifierExceptionHandler(val sendError: (String) -> Unit) {
private val logTag = Util.getLogTag(javaClass.simpleName)

fun handleException(e: VerifierException){
Log.e(logTag, "Verifier Exception: $e")
when (e.cause?.javaClass?.simpleName) {
StateHandlerException::class.simpleName -> Log.e(logTag, "Verifier State Handler Exception: ${e.cause}")
TransferHandlerException::class.simpleName -> Log.e(logTag, "Verifier Transfer Handler Exception: ${e.cause}")
else -> Log.e(logTag, "Verifier Exception: $e")
}
sendError(e.message ?: "Something went wrong in Verifier: ${e.cause}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.os.Looper
import android.os.Message
import android.util.Log
import io.mosip.tuvali.ble.peripheral.Peripheral
import io.mosip.tuvali.openid4vpble.exception.exception.TransferHandlerException
import io.mosip.tuvali.openid4vpble.exception.TransferHandlerException
import io.mosip.tuvali.transfer.Assembler
import io.mosip.tuvali.transfer.TransferReportRequest
import io.mosip.tuvali.transfer.TransferReport
Expand All @@ -14,7 +14,6 @@ import io.mosip.tuvali.verifier.exception.CorruptedChunkReceivedException
import io.mosip.tuvali.verifier.exception.TooManyFailureChunksException
import io.mosip.tuvali.verifier.transfer.message.*
import java.util.*
import kotlin.math.ceil
import io.mosip.tuvali.transfer.Util.Companion.getLogTag

class TransferHandler(looper: Looper, private val peripheral: Peripheral, private val transferListener: ITransferListener, val serviceUUID: UUID) : Handler(looper) {
Expand Down Expand Up @@ -132,7 +131,7 @@ class TransferHandler(looper: Looper, private val peripheral: Peripheral, privat
super.dispatchMessage(msg)
} catch (e: Exception) {
transferListener.onException(TransferHandlerException("Exception in Verifier Transfer Handler", e))
Log.d(logTag, "dispatchMessage " + e.message)
Log.e(logTag, "dispatchMessage " + e.message)
}
}

Expand Down
7 changes: 4 additions & 3 deletions android/src/main/java/io/mosip/tuvali/wallet/Wallet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ import java.security.SecureRandom
import java.util.*
import io.mosip.tuvali.transfer.Util.Companion.getLogTag
import io.mosip.tuvali.wallet.exception.TransferFailedException
import java.lang.Thread.setDefaultUncaughtExceptionHandler
import io.mosip.tuvali.wallet.exception.WalletException

private const val MTU_REQUEST_RETRY_DELAY_TIME_IN_MILLIS = 500L

class Wallet(
context: Context,
private val messageResponseListener: (String, String) -> Unit,
private val eventResponseListener: (String) -> Unit,
private val onBLEException: (Exception) -> Unit
private val handleException: (Exception) -> Unit
) : ICentralListener, ITransferListener {
private val logTag = getLogTag(javaClass.simpleName)

Expand Down Expand Up @@ -241,6 +241,7 @@ class Wallet(
transferHandler.sendMessage(ResponseChunkWriteFailureMessage(err))
}
GattService.TRANSFER_REPORT_REQUEST_CHAR_UUID -> {
//TODO: implement a retry strategy similar to ios if the transfer report request write fails
transferHandler.sendMessage(ResponseTransferFailureMessage("Failed to request report with err: $err"))
}
}
Expand Down Expand Up @@ -305,7 +306,7 @@ class Wallet(
}

override fun onException(exception: Exception) {
onBLEException(exception)
handleException(WalletException("Exception in Wallet", exception))
}

override fun onClosed() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package io.mosip.tuvali.wallet.exception

open class WalletException(message: String): Exception(message)
open class WalletException(message: String, cause: Throwable? = null): Exception(message, cause)
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package io.mosip.tuvali.wallet.exception

import android.util.Log
import io.mosip.tuvali.openid4vpble.exception.StateHandlerException
import io.mosip.tuvali.openid4vpble.exception.TransferHandlerException
import io.mosip.tuvali.transfer.Util

class WalletExceptionHandler(val sendError: (String) -> Unit) {
private val logTag = Util.getLogTag(javaClass.simpleName)

fun handleException(e: WalletException){
Log.e(logTag, "Wallet Exception: $e")
when (e.cause?.javaClass?.simpleName) {
StateHandlerException::class.simpleName -> Log.e(logTag, "Wallet State Handler Exception: ${e.cause}")
TransferHandlerException::class.simpleName -> Log.e(logTag, "Wallet Transfer Handler Exception: ${e.cause}")
else -> Log.e(logTag, "Wallet Exception: $e")
}
sendError(e.message ?: "Something went wrong in Wallet: ${e.cause}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.os.Looper
import android.os.Message
import android.util.Log
import io.mosip.tuvali.ble.central.Central
import io.mosip.tuvali.openid4vpble.exception.exception.TransferHandlerException
import io.mosip.tuvali.openid4vpble.exception.TransferHandlerException
import io.mosip.tuvali.transfer.*
import io.mosip.tuvali.verifier.GattService
import io.mosip.tuvali.wallet.transfer.message.*
Expand Down Expand Up @@ -175,7 +175,7 @@ class TransferHandler(looper: Looper, private val central: Central, val serviceU
super.dispatchMessage(msg)
} catch (e: Exception) {
transferListener.onException(TransferHandlerException("Exception in Central transfer Handler", e))
Log.d(logTag, "dispatchMessage " + e.message)
Log.e(logTag, "dispatchMessage " + e.message)
}
}

Expand Down

0 comments on commit 474e1ab

Please sign in to comment.