Compare commits

...

2 commits

Author SHA1 Message Date
4b737a6988
feat: simplified error handling 2025-07-17 18:43:28 +08:00
6d43a9207c chore: simplify pretty print json string (#201)
All checks were successful
/ build-debug (push) Successful in 5m7s
https://developer.android.com/reference/org/json/JSONObject
Reviewed-on: #201
Co-authored-by: septs <github@septs.pw>
Co-committed-by: septs <github@septs.pw>
2025-07-16 14:24:05 +02:00
4 changed files with 168 additions and 71 deletions

View file

@ -8,6 +8,7 @@ import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import im.angry.openeuicc.common.R import im.angry.openeuicc.common.R
import im.angry.openeuicc.util.* import im.angry.openeuicc.util.*
import org.json.JSONObject
import java.util.Date import java.util.Date
class DownloadWizardDiagnosticsFragment : DownloadWizardActivity.DownloadWizardStepFragment() { class DownloadWizardDiagnosticsFragment : DownloadWizardActivity.DownloadWizardStepFragment() {
@ -86,9 +87,10 @@ class DownloadWizardDiagnosticsFragment : DownloadWizardActivity.DownloadWizardS
ret.appendLine() ret.appendLine()
val str = resp.data.decodeToString(throwOnInvalidSequence = false) val str = resp.data.decodeToString(throwOnInvalidSequence = false)
ret.appendLine( ret.appendLine(
if (str.startsWith('{')) { if (str.startsWith('{')) {
str.prettyPrintJson() JSONObject(str).toString(2)
} else { } else {
str str
} }

View file

@ -0,0 +1,145 @@
package im.angry.openeuicc.ui.wizard
import androidx.annotation.StringRes
import im.angry.openeuicc.common.R
import net.typeblog.lpac_jni.LocalProfileAssistant
import org.json.JSONObject
import java.net.NoRouteToHostException
import java.net.PortUnreachableException
import java.net.SocketException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import javax.net.ssl.SSLException
object SimplifiedErrorHandling {
enum class ErrorCode(@StringRes val titleResId: Int, @StringRes val suggestResId: Int?) {
ICCIDAlready(
R.string.download_wizard_error_iccid_already,
R.string.download_wizard_error_suggest_profile_installed
),
InsufficientMemory(
R.string.download_wizard_error_insufficient_memory,
R.string.download_wizard_error_suggest_insufficient_memory
),
UnsupportedProfile(
R.string.download_wizard_error_unsupported_profile,
null
),
CardInternalError(
R.string.download_wizard_error_card_internal_error,
null
),
EIDMismatch(
R.string.download_wizard_error_eid_mismatch,
R.string.download_wizard_error_suggest_contact_reissue
),
UnreleasedProfile(
R.string.download_wizard_error_profile_unreleased,
R.string.download_wizard_error_suggest_contact_reissue
),
MatchingIDRefused(
R.string.download_wizard_error_matching_id_refused,
R.string.download_wizard_error_suggest_contact_carrier
),
ProfileRetriesExceeded(
R.string.download_wizard_error_profile_retries_exceeded,
R.string.download_wizard_error_suggest_contact_carrier
),
ConfirmationCodeMissing(
R.string.download_wizard_error_confirmation_code_missing,
R.string.download_wizard_error_suggest_contact_carrier
),
ConfirmationCodeRefused(
R.string.download_wizard_error_confirmation_code_refused,
R.string.download_wizard_error_suggest_contact_carrier
),
ConfirmationCodeRetriesExceeded(
R.string.download_wizard_error_confirmation_code_retries_exceeded,
R.string.download_wizard_error_suggest_contact_carrier
),
ProfileExpired(
R.string.download_wizard_error_profile_expired,
R.string.download_wizard_error_suggest_contact_carrier
),
UnknownHost(
R.string.download_wizard_error_unknown_hostname,
null
),
NetworkUnreachable(
R.string.download_wizard_error_network_unreachable,
R.string.download_wizard_error_suggest_network_unreachable
),
TLSError(
R.string.download_wizard_error_tls_certificate,
null
)
}
private val httpErrors = buildMap {
// Stage: AuthenticateClient
put("8.1" to "4.8", ErrorCode.InsufficientMemory)
put("8.1.1" to "3.8", ErrorCode.EIDMismatch)
put("8.2" to "1.2", ErrorCode.UnreleasedProfile)
put("8.2.6" to "3.8", ErrorCode.MatchingIDRefused)
put("8.8.5" to "6.4", ErrorCode.ProfileRetriesExceeded)
// Stage: GetBoundProfilePackage
put("8.2.7" to "2.2", ErrorCode.ConfirmationCodeMissing)
put("8.2.7" to "3.8", ErrorCode.ConfirmationCodeRefused)
put("8.2.7" to "6.4", ErrorCode.ConfirmationCodeRetriesExceeded)
// Stage: AuthenticateClient, GetBoundProfilePackage
put("8.8.5" to "4.10", ErrorCode.ProfileExpired)
}
fun toSimplifiedDownloadError(exc: LocalProfileAssistant.ProfileDownloadException) = when {
exc.lpaErrorReason != "ES10B_ERROR_REASON_UNDEFINED" -> toSimplifiedLPAErrorReason(exc.lpaErrorReason)
exc.lastHttpResponse?.rcode == 200 -> toSimplifiedHTTPResponse(exc.lastHttpResponse!!)
exc.lastHttpException != null -> toSimplifiedHTTPException(exc.lastHttpException!!)
exc.lastApduResponse != null -> toSimplifiedAPDUResponse(exc.lastApduResponse!!)
else -> null
}
private fun toSimplifiedLPAErrorReason(reason: String) = when (reason) {
"ES10B_ERROR_REASON_UNSUPPORTED_CRT_VALUES" -> ErrorCode.UnsupportedProfile
"ES10B_ERROR_REASON_UNSUPPORTED_REMOTE_OPERATION_TYPE" -> ErrorCode.UnsupportedProfile
"ES10B_ERROR_REASON_UNSUPPORTED_PROFILE_CLASS" -> ErrorCode.UnsupportedProfile
"ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_ICCID_ALREADY_EXISTS_ON_EUICC" -> ErrorCode.ICCIDAlready
"ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_INSUFFICIENT_MEMORY_FOR_PROFILE" -> ErrorCode.InsufficientMemory
"ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_INTERRUPTION" -> ErrorCode.CardInternalError
"ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_PE_PROCESSING_ERROR" -> ErrorCode.CardInternalError
else -> null
}
private fun toSimplifiedHTTPResponse(response: net.typeblog.lpac_jni.HttpInterface.HttpResponse): ErrorCode? {
if (response.data.first().toInt() != '{'.code) return null
val response = JSONObject(response.data.decodeToString())
val statusCodeData = response.optJSONObject("header")
?.optJSONObject("functionExecutionStatus")
?.optJSONObject("statusCodeData")
?: return null
val subjectCode = statusCodeData.optString("subjectCode")
val reasonCode = statusCodeData.optString("reasonCode")
return httpErrors[subjectCode to reasonCode]
}
private fun toSimplifiedHTTPException(exc: Exception) = when (exc) {
is SSLException -> ErrorCode.TLSError
is UnknownHostException -> ErrorCode.UnknownHost
is NoRouteToHostException -> ErrorCode.NetworkUnreachable
is PortUnreachableException -> ErrorCode.NetworkUnreachable
is SocketTimeoutException -> ErrorCode.NetworkUnreachable
is SocketException -> exc.message
?.contains("Connection reset", ignoreCase = true)
?.let { if (it) ErrorCode.NetworkUnreachable else null }
else -> null
}
private fun toSimplifiedAPDUResponse(resp: ByteArray): ErrorCode? {
val isSuccess = resp.size >= 2 &&
resp[resp.size - 2] == 0x90.toByte() &&
resp[resp.size - 1] == 0x00.toByte()
if (isSuccess) return null
return ErrorCode.CardInternalError
}
}

View file

@ -41,73 +41,3 @@ fun parseIsdrAidList(s: String): List<ByteArray> =
.filter(String::isNotEmpty) .filter(String::isNotEmpty)
.mapNotNull { runCatching(it::decodeHex).getOrNull() } .mapNotNull { runCatching(it::decodeHex).getOrNull() }
.ifEmpty { listOf(EUICC_DEFAULT_ISDR_AID.decodeHex()) } .ifEmpty { listOf(EUICC_DEFAULT_ISDR_AID.decodeHex()) }
fun String.prettyPrintJson(): String {
val ret = StringBuilder()
var inQuotes = false
var escaped = false
val indentSymbolStack = ArrayDeque<Char>()
val addNewLine = {
ret.append('\n')
repeat(indentSymbolStack.size) {
ret.append('\t')
}
}
var lastChar = ' '
for (c in this) {
when {
!inQuotes && (c == '{' || c == '[') -> {
ret.append(c)
indentSymbolStack.addLast(c)
addNewLine()
}
!inQuotes && (c == '}' || c == ']') -> {
indentSymbolStack.removeLast()
if (lastChar != ',') {
addNewLine()
}
ret.append(c)
}
!inQuotes && c == ',' -> {
ret.append(c)
addNewLine()
}
!inQuotes && c == ':' -> {
ret.append(c)
ret.append(' ')
}
inQuotes && c == '\\' -> {
ret.append(c)
escaped = true
continue
}
!escaped && c == '"' -> {
ret.append(c)
inQuotes = !inQuotes
}
!inQuotes && c == ' ' -> {
// Do nothing -- we ignore spaces outside of quotes by default
// This is to ensure predictable formatting
}
else -> ret.append(c)
}
if (escaped) {
escaped = false
}
lastChar = c
}
return ret.toString()
}

View file

@ -102,6 +102,26 @@
<string name="download_wizard_diagnostics_last_apdu_exception">Last APDU exception:</string> <string name="download_wizard_diagnostics_last_apdu_exception">Last APDU exception:</string>
<string name="download_wizard_diagnostics_save">Save</string> <string name="download_wizard_diagnostics_save">Save</string>
<string name="download_wizard_diagnostics_file_template">Diagnostics at %s</string> <string name="download_wizard_diagnostics_file_template">Diagnostics at %s</string>
<string name="download_wizard_error_iccid_already">This eSIM profile is installed, Cannot be reinstalled.</string>
<string name="download_wizard_error_insufficient_memory">Sorry, The remaining capacity of this eSIM chip cannot be used to install this eSIM profile.</string>
<string name="download_wizard_error_unsupported_profile">Sorry, This eSIM profile is unsupported.</string>
<string name="download_wizard_error_card_internal_error">An error occurred inside the card.</string>
<string name="download_wizard_error_eid_mismatch">This eSIM profile has been installed on another device.</string>
<string name="download_wizard_error_profile_unreleased">This eSIM profile has been unreleased.</string>
<string name="download_wizard_error_matching_id_refused">This eSIM activation code is invalid.</string>
<string name="download_wizard_error_profile_retries_exceeded">The maximum number of retries for the eSIM profile has been exceeded.</string>
<string name="download_wizard_error_confirmation_code_missing">Please enter the confirmation code to continue.</string>
<string name="download_wizard_error_confirmation_code_refused">The confirmation code you entered is invalid.</string>
<string name="download_wizard_error_profile_expired">This eSIM profile has been expired.</string>
<string name="download_wizard_error_confirmation_code_retries_exceeded">The maximum number of retries for the Confirmation Code has been exceeded.</string>
<string name="download_wizard_error_unknown_hostname">Unknown SM-DP+ address</string>
<string name="download_wizard_error_network_unreachable">The current network is unreachable</string>
<string name="download_wizard_error_tls_certificate">TLS certificate error, this eSIM profile is not supported</string>
<string name="download_wizard_error_suggest_profile_installed">You are trying to reinstall an already installed eSIM profile</string>
<string name="download_wizard_error_suggest_insufficient_memory">Please delete an eSIM profile and try again</string>
<string name="download_wizard_error_suggest_contact_carrier">Please contact your carrier for assistance.</string>
<string name="download_wizard_error_suggest_contact_reissue">Please contact your carrier to reissue this eSIM profile.</string>
<string name="download_wizard_error_suggest_network_unreachable">The current network is unavailable. Please try again after changing the network.</string>
<string name="logs_saved_message">Logs have been saved to the selected path. Would you like to share the log through another app?</string> <string name="logs_saved_message">Logs have been saved to the selected path. Would you like to share the log through another app?</string>