Skip to content

Commit

Permalink
Merge pull request #44 from tw-mosip/add-error-code
Browse files Browse the repository at this point in the history
Add error code to OnError Event
  • Loading branch information
krishnakumar4a4 authored Apr 24, 2023
2 parents 062df42 + bced3a5 commit e2f829d
Show file tree
Hide file tree
Showing 45 changed files with 312 additions and 111 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ node_modules/
npm-debug.log
yarn-debug.log
yarn-error.log
.yalc

# BUCK
buck-out/
Expand All @@ -72,4 +73,4 @@ ios/libs
ios/generated/
android/src/main/jniLibs/
rust/purerust/target/
rust/rustlib_binding/target/
rust/rustlib_binding/target/
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.mosip.tuvali.ble.central

import android.bluetooth.BluetoothDevice
import android.bluetooth.le.ScanRecord
import io.mosip.tuvali.exception.BLEException
import java.util.*

interface ICentralListener {
Expand All @@ -21,5 +22,5 @@ interface ICentralListener {
fun onSubscriptionFailure(charUUID: UUID, err: Int)
fun onNotificationReceived(charUUID: UUID, value: ByteArray?)
fun onClosed()
fun onException(exception: Exception)
fun onException(exception: BLEException)
}
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.StateHandlerException
import io.mosip.tuvali.ble.exception.CentralStateHandlerException
import io.mosip.tuvali.transfer.Util.Companion.getLogTag

class StateHandler(
Expand Down Expand Up @@ -165,14 +165,14 @@ class StateHandler(
currentState = States.Writing
}
IMessage.CentralStates.WRITE_SUCCESS.ordinal -> {
val writeSuccessMessage = msg.obj as WriteSuccessMessage;
val writeSuccessMessage = msg.obj as WriteSuccessMessage
Log.d(logTag, "Completed writing to ${writeSuccessMessage.charUUID} successfully")

listener.onWriteSuccess(writeSuccessMessage.device, writeSuccessMessage.charUUID)
currentState = States.Connected
}
IMessage.CentralStates.WRITE_FAILURE.ordinal -> {
val writeFailureMessage = msg.obj as WriteFailureMessage;
val writeFailureMessage = msg.obj as WriteFailureMessage

Log.d(logTag, "write failed for ${writeFailureMessage.charUUID} due to ${writeFailureMessage.err}")
listener.onWriteFailed(writeFailureMessage.device, writeFailureMessage.charUUID, writeFailureMessage.err)
Expand Down Expand Up @@ -256,7 +256,7 @@ class StateHandler(
try {
super.dispatchMessage(msg)
} catch (e: Exception) {
listener.onException(StateHandlerException("Exception in Central State Handler", e))
listener.onException(CentralStateHandlerException("Exception in Central State Handler", e))
Log.e(logTag, "dispatchMessage " + e.message)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.mosip.tuvali.ble.exception

import io.mosip.tuvali.exception.BLEException
import io.mosip.tuvali.exception.ErrorCode

class CentralStateHandlerException(message: String, cause: Exception): BLEException(message, cause,
ErrorCode.CentralStateHandlerException
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.mosip.tuvali.ble.exception

import io.mosip.tuvali.exception.BLEException
import io.mosip.tuvali.exception.ErrorCode

class PeripheralStateHandlerException(message: String, cause: Exception): BLEException(message, cause,
ErrorCode.PeripheralStateHandlerException
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.mosip.tuvali.ble.peripheral

import io.mosip.tuvali.exception.BLEException
import java.util.UUID

interface IPeripheralListener {
Expand All @@ -11,5 +12,5 @@ interface IPeripheralListener {
fun onSendDataNotified(uuid: UUID, isSent: Boolean)
fun onClosed()
fun onMTUChanged(mtu: Int)
fun onException(exception: Exception)
fun onException(exception: BLEException)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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

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,9 +8,9 @@ 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.StateHandlerException
import io.mosip.tuvali.ble.peripheral.state.message.*
import io.mosip.tuvali.transfer.Util.Companion.getLogTag
import io.mosip.tuvali.ble.exception.PeripheralStateHandlerException

class StateHandler(
looper: Looper,
Expand Down Expand Up @@ -157,7 +157,7 @@ class StateHandler(
try {
super.dispatchMessage(msg)
} catch (e: Exception) {
peripheralListener.onException(StateHandlerException("Exception in Peripheral State Handler", e))
peripheralListener.onException(PeripheralStateHandlerException("Exception in Peripheral State Handler", e))
Log.e(logTag, "dispatchMessage " + e.message)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.mosip.tuvali.common.safeExecute

import io.mosip.tuvali.openid4vpble.exception.OpenIdBLEExceptionHandler
import io.mosip.tuvali.exception.handlers.OpenIdBLEExceptionHandler

class TryExecuteSync(private val bleExceptionHandler: OpenIdBLEExceptionHandler) {
private val mutex = Object()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.mosip.tuvali.exception

open class BLEException(message: String, cause: Exception?, val errorCode: ErrorCode): RuntimeException(message, cause) {
}
29 changes: 29 additions & 0 deletions android/src/main/java/io/mosip/tuvali/exception/ErrorCode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.mosip.tuvali.exception

// Error Code format
// <Component(2)+Role(1)>(3char)_<Stage>(3char)_<Number>(3char) Eg: TVW-CON-001
// Stage --> CON(Connection) | KEX(Key Exchange) | ENC(Encryption) | TRA(Transfer) | REP(Report) | DEC(Decryption)
// Component+ROLE --> TVW(Tuvali+Wallet) | TVV(Tuvali+Verifier) | TUV(Tuvali where role is unknown)
// UNK --> If stage is not known

enum class ErrorCode(val code: String) {
UnknownException("TUV_UNK_001"),

WalletUnknownException("TVW_UNK_001"),
CentralStateHandlerException("TVW_UNK_002"),
WalletTransferHandlerException("TVW_UNK_003"),

VerifierUnknownException("TVV_UNK_001"),
PeripheralStateHandlerException("TVV_UNK_002"),
VerifierTransferHandlerException("TVV_UNK_003"),

MTUNegotiationException("TVW_CON_001"),
//TODO: Create specific error codes for the below exception
TransferFailedException("TVW_REP_001"),

UnsupportedMTUSizeException("TVV_CON_001"),
CorruptedChunkReceivedException("TVV_TRA_001"),
TooManyFailureChunksException("TVV_TRA_002"),
}


13 changes: 13 additions & 0 deletions android/src/main/java/io/mosip/tuvali/exception/ExceptionUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.mosip.tuvali.exception

class ExceptionUtils {
companion object {
fun getRootBLECause(e: BLEException): BLEException {
var cause: BLEException = e
while (cause.cause != null && cause.cause is BLEException) {
cause = cause.cause as BLEException
}
return cause
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.mosip.tuvali.exception

class UnknownException(message: String, cause: Exception): BLEException(message, cause, ErrorCode.UnknownException) {
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package io.mosip.tuvali.openid4vpble.exception
package io.mosip.tuvali.exception.handlers

import android.util.Log
import com.facebook.react.bridge.Callback
import io.mosip.tuvali.exception.BLEException
import io.mosip.tuvali.exception.ErrorCode
import io.mosip.tuvali.exception.ExceptionUtils
import io.mosip.tuvali.exception.UnknownException

import io.mosip.tuvali.transfer.Util
import io.mosip.tuvali.verifier.exception.VerifierException
import io.mosip.tuvali.verifier.exception.VerifierExceptionHandler
import io.mosip.tuvali.wallet.exception.WalletException
import io.mosip.tuvali.wallet.exception.WalletExceptionHandler

class OpenIdBLEExceptionHandler(private val sendError: (String) -> Unit, private val stopBle: (Callback) -> Unit) {
class OpenIdBLEExceptionHandler(private val sendError: (String, ErrorCode) -> Unit, private val stopBle: (Callback) -> Unit) {
private val logTag = Util.getLogTag(javaClass.simpleName)
private var walletExceptionHandler: WalletExceptionHandler = WalletExceptionHandler(sendError);
private var verifierExceptionHandler: VerifierExceptionHandler = VerifierExceptionHandler(sendError);
private var walletExceptionHandler: WalletExceptionHandler = WalletExceptionHandler(sendError)
private var verifierExceptionHandler: VerifierExceptionHandler = VerifierExceptionHandler(sendError)

private fun handleWalletException(e: WalletException) {
walletExceptionHandler.handleException(e)
Expand All @@ -21,7 +24,7 @@ class OpenIdBLEExceptionHandler(private val sendError: (String) -> Unit, private
verifierExceptionHandler.handleException(e)
}

fun handleException(e: Throwable) {
fun handleException(e: Exception) {
when (e) {
is WalletException -> {
handleWalletException(e)
Expand All @@ -30,7 +33,7 @@ class OpenIdBLEExceptionHandler(private val sendError: (String) -> Unit, private
handleVerifierException(e)
}
else -> {
handleUnknownException(e)
handleUnknownException(UnknownException("Unknown Exception in Tuvali", e))
}
}

Expand All @@ -41,8 +44,10 @@ class OpenIdBLEExceptionHandler(private val sendError: (String) -> Unit, private
}
}

private fun handleUnknownException(e: Throwable) {
Log.e(logTag, "Unknown exception: $e")
sendError(e.message ?: "Something went wrong in BLE: ${e.cause}")
private fun handleUnknownException(e: BLEException) {
val rootCause = ExceptionUtils.getRootBLECause(e)

Log.e(logTag, "Handling Unknown Exception: ", e)
sendError(e.message ?: "Something went wrong in BLE: $rootCause", rootCause.errorCode)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.mosip.tuvali.exception.handlers

import android.util.Log
import io.mosip.tuvali.exception.ErrorCode
import io.mosip.tuvali.exception.ExceptionUtils
import io.mosip.tuvali.transfer.Util
import io.mosip.tuvali.verifier.exception.VerifierException

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

fun handleException(e: VerifierException){
val rootCause = ExceptionUtils.getRootBLECause(e)

Log.e(logTag, "Handling Verifier Exception: ", e)
sendError(e.message ?: "Something went wrong in Verifier: $rootCause", rootCause.errorCode)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.mosip.tuvali.exception.handlers

import android.util.Log
import io.mosip.tuvali.exception.ErrorCode
import io.mosip.tuvali.exception.ExceptionUtils
import io.mosip.tuvali.transfer.Util
import io.mosip.tuvali.wallet.exception.WalletException

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

fun handleException(e: WalletException) {
val rootCause = ExceptionUtils.getRootBLECause(e)

Log.e(logTag, "Handling Wallet Exception: ", e)
sendError(e.message ?: "Something went wrong in Wallet: $rootCause", rootCause.errorCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import android.util.Log
import com.facebook.react.bridge.*
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
import io.mosip.tuvali.common.safeExecute.TryExecuteSync
import io.mosip.tuvali.openid4vpble.exception.OpenIdBLEExceptionHandler
import org.json.JSONObject
import io.mosip.tuvali.exception.handlers.OpenIdBLEExceptionHandler
import io.mosip.tuvali.exception.ErrorCode
import io.mosip.tuvali.transfer.Util.Companion.getLogTag
import io.mosip.tuvali.verifier.Verifier
import io.mosip.tuvali.wallet.Wallet
import org.json.JSONObject

class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
private val logTag = getLogTag(javaClass.simpleName)
private var verifier: Verifier? = null
private var wallet: Wallet? = null
private var bleExceptionHandler = OpenIdBLEExceptionHandler(this::emitNearbyErrorEvent, this::stopBLE)
private val tryExecuteSync = TryExecuteSync(bleExceptionHandler);
private val tryExecuteSync = TryExecuteSync(bleExceptionHandler)

//Inji contract requires double quotes around the states.
enum class VCResponseStates(val value: String) {
Expand Down Expand Up @@ -193,9 +194,10 @@ class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
emitEvent("EVENT_NEARBY", writableMap)
}

private fun emitNearbyErrorEvent(message: String) {
private fun emitNearbyErrorEvent(message: String, errorCode: ErrorCode) {
val writableMap = Arguments.createMap()
writableMap.putString("message", message)
writableMap.putString("code", errorCode.code.toString())
writableMap.putString("type", "onError")
emitEvent("EVENT_NEARBY", writableMap)
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 5 additions & 4 deletions android/src/main/java/io/mosip/tuvali/verifier/Verifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.mosip.tuvali.cryptography.SecretsTranslator
import io.mosip.tuvali.cryptography.VerifierCryptoBox
import io.mosip.tuvali.cryptography.VerifierCryptoBoxBuilder
import io.mosip.tuvali.openid4vpble.Openid4vpBleModule
import io.mosip.tuvali.exception.BLEException
import io.mosip.tuvali.transfer.ByteCount.FourBytes
import io.mosip.tuvali.transfer.TransferReportRequest
import io.mosip.tuvali.transfer.Util
Expand All @@ -33,10 +34,10 @@ class Verifier(
context: Context,
private val messageResponseListener: (String, String) -> Unit,
private val eventResponseListener: (String) -> Unit,
private val handleException: (Throwable) -> Unit
private val handleException: (BLEException) -> Unit
) :
IPeripheralListener, ITransferListener {
private var secretsTranslator: SecretsTranslator? = null;
private var secretsTranslator: SecretsTranslator? = null
private val logTag = getLogTag(javaClass.simpleName)
private var publicKey: ByteArray = byteArrayOf()
private lateinit var walletPubKey: ByteArray
Expand Down Expand Up @@ -201,7 +202,7 @@ class Verifier(
}
}

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

Expand Down Expand Up @@ -260,7 +261,7 @@ class Verifier(
} catch (e: Exception) {
Log.e(logTag, "failed to decrypt data of size ${data.size}, with exception: ${e.message}, stacktrace: ${e.stackTraceToString()}")
//Re-Throwing for the exception handler to handle this again and let Higher layer know.
throw e;
throw e
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package io.mosip.tuvali.verifier.exception

class CorruptedChunkReceivedException(size: Int, receivedSeqNumber: Int, receivedMtuSize: Int) : VerifierException(
"size: $size, receivedSeqNumber: $receivedSeqNumber, receivedMtuSize: $receivedMtuSize"
) {}
import io.mosip.tuvali.exception.BLEException
import io.mosip.tuvali.exception.ErrorCode

class CorruptedChunkReceivedException(size: Int, receivedSeqNumber: Int, receivedMtuSize: Int) : BLEException(
"size: $size, receivedSeqNumber: $receivedSeqNumber, receivedMtuSize: $receivedMtuSize",
null,
ErrorCode.CorruptedChunkReceivedException
)

This file was deleted.

Loading

0 comments on commit e2f829d

Please sign in to comment.