Skip to content

Commit

Permalink
refactor: centralize Android NFC method exception handling
Browse files Browse the repository at this point in the history
Fixes #168
  • Loading branch information
knthm committed May 31, 2024
1 parent 0e53a9d commit 27fe9ba
Showing 1 changed file with 33 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
result.error("500", "Failed to post job to NFC Handler thread.", null)
}
}

private fun handleException(ex: Exception, result: Result?, desc: String) {
Log.e(TAG, "$desc error", ex)
val excMessage = ex.localizedMessage
when (ex) {
is IOException -> result?.error("500", "Communication error", excMessage )
is SecurityException -> result?.error("503", "Tag already removed", excMessage)
is FormatException -> result?.error("400", "NDEF format error", excMessage)
is InvocationTargetException -> result?.error("500", "Communication error", excMessage)
is IllegalArgumentException -> result?.error("400", "Command format error", excMessage)
is NoSuchMethodException -> result?.error("405", "Transceive not supported for this type of card", excMessage)
else -> result?.error("500", "Unhandled error", excMessage)
}
}
}

override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
Expand Down Expand Up @@ -147,10 +161,8 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
if (ndefTech != null && ndefTech.isConnected) {
ndefTech.close()
}
} catch (ex: SecurityException) {
Log.e(TAG, "Tag already removed", ex)
} catch (ex: IOException) {
Log.e(TAG, "Close tag error", ex)
} catch (ex: Exception) {
handleException(ex , result, "Close tag")
}
if (activity.get() != null) {
nfcAdapter.disableReaderMode(activity.get())
Expand Down Expand Up @@ -181,21 +193,8 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
is String -> result.success(resp.toHexString())
else -> result.success(resp)
}
} catch (ex: SecurityException) {
Log.e(TAG, "Transceive Error: $sendingHex", ex)
result.error("503", "Tag already removed", ex.localizedMessage)
} catch (ex: IOException) {
Log.e(TAG, "Transceive Error: $sendingHex", ex)
result.error("500", "Communication error", ex.localizedMessage)
} catch (ex: InvocationTargetException) {
Log.e(TAG, "Transceive Error: $sendingHex", ex.cause ?: ex)
result.error("500", "Communication error", ex.cause?.localizedMessage)
} catch (ex: IllegalArgumentException) {
Log.e(TAG, "Command Error: $sendingHex", ex)
result.error("400", "Command format error", ex.localizedMessage)
} catch (ex: NoSuchMethodException) {
Log.e(TAG, "Transceive not supported: $sendingHex", ex)
result.error("405", "Transceive not supported for this type of card", null)
} catch (ex: Exception) {
handleException(ex , result, "Transceive: $sendingHex")
}
}
}
Expand Down Expand Up @@ -233,15 +232,8 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
}
}
result.success(JSONArray(parsedMessages).toString())
} catch (ex: SecurityException) {
Log.e(TAG, "Read NDEF Error", ex)
result.error("503", "Tag already removed", ex.localizedMessage)
} catch (ex: IOException) {
Log.e(TAG, "Read NDEF Error", ex)
result.error("500", "Communication error", ex.localizedMessage)
} catch (ex: FormatException) {
Log.e(TAG, "NDEF Format Error", ex)
result.error("400", "NDEF format error", ex.localizedMessage)
} catch (ex: Exception) {
handleException(ex , result, "Read NDEF")
}
}
}
Expand Down Expand Up @@ -280,15 +272,8 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
val message = NdefMessage(records)
ndef.writeNdefMessage(message)
result.success("")
} catch (ex: SecurityException) {
Log.e(TAG, "Write NDEF Error", ex)
result.error("503", "Tag already removed", ex.localizedMessage)
} catch (ex: IOException) {
Log.e(TAG, "Write NDEF Error", ex)
result.error("500", "Communication error", ex.localizedMessage)
} catch (ex: FormatException) {
Log.e(TAG, "NDEF Format Error", ex)
result.error("400", "NDEF format error", ex.localizedMessage)
} catch (ex: Exception) {
handleException(ex , result, "Write NDEF")
}
}
}
Expand All @@ -308,12 +293,8 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
} else {
result.error("500", "Failed to lock NDEF tag", null)
}
} catch (ex: SecurityException) {
Log.e(TAG, "Lock NDEF Error", ex)
result.error("503", "Tag already removed", ex.localizedMessage)
} catch (ex: IOException) {
Log.e(TAG, "Lock NDEF Error", ex)
result.error("500", "Communication error", ex.localizedMessage)
} catch (ex: Exception) {
handleException(ex , result, "Lock NDEF")
}
}
}
Expand Down Expand Up @@ -349,9 +330,8 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
} else {
result.error("400", "No keys provided", null)
}
} catch (ex: IOException) {
Log.e(TAG, "Authenticate block error", ex)
result.error("500", "Authentication error", ex.localizedMessage)
} catch (ex: Exception) {
handleException(ex , result, "Authenticate sector")
}
}
}
Expand All @@ -372,9 +352,8 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
try {
switchTechnology(tagTech, ndefTechnology)
tagTech.readBlock(index, result)
} catch (ex: IOException) {
Log.e(TAG, "Read block error", ex)
result.error("500", "Communication error", ex.localizedMessage)
} catch (ex: Exception) {
handleException(ex , result, "Read block")
}
}
}
Expand All @@ -396,9 +375,9 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
val tag = tagTech as MifareClassic
switchTechnology(tagTech, ndefTechnology)
result.success(tag.readSector(index))
} catch (ex: IOException) {
Log.e(TAG, "Read sector error", ex)
result.error("500", "Communication error", ex.localizedMessage) }
} catch (ex: Exception) {
handleException(ex , result, "Read sector")
}
}
}

Expand Down Expand Up @@ -428,9 +407,8 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
try {
switchTechnology(tagTech, ndefTechnology)
tagTech.writeBlock(index, bytes, result)
} catch (ex: IOException) {
Log.e(TAG, "Read block error", ex)
result.error("500", "Communication error", ex.localizedMessage)
} catch (ex: Exception) {
handleException(ex , result, "Write block")
}
}
}
Expand Down

0 comments on commit 27fe9ba

Please sign in to comment.