fix(android): handle unreachable gateway gracefully
Previously, if the gateway was unreachable (wrong IP, offline, etc.), the Android app would crash with an unhandled socket exception. Changes: - Wrap socket.connect() in try/catch to handle connection failures - Return PairResult with error message instead of crashing - Display actual error message in status text instead of generic 'pairing required' This gives users useful feedback like 'Connection refused' or 'Network is unreachable' instead of a crash.
This commit is contained in:
committed by
Peter Steinberger
parent
fbaa109a3a
commit
12186e14a9
@@ -433,7 +433,8 @@ class NodeRuntime(context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!resolved.ok || resolved.token.isNullOrBlank()) {
|
if (!resolved.ok || resolved.token.isNullOrBlank()) {
|
||||||
_statusText.value = "Failed: pairing required"
|
val errorMessage = resolved.error?.trim().orEmpty().ifEmpty { "pairing required" }
|
||||||
|
_statusText.value = "Failed: $errorMessage"
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ class BridgePairingClient {
|
|||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
val socket = Socket()
|
val socket = Socket()
|
||||||
socket.tcpNoDelay = true
|
socket.tcpNoDelay = true
|
||||||
|
try {
|
||||||
socket.connect(InetSocketAddress(endpoint.host, endpoint.port), 8_000)
|
socket.connect(InetSocketAddress(endpoint.host, endpoint.port), 8_000)
|
||||||
socket.soTimeout = 60_000
|
socket.soTimeout = 60_000
|
||||||
|
|
||||||
@@ -51,7 +52,6 @@ class BridgePairingClient {
|
|||||||
|
|
||||||
fun sendJson(obj: JsonObject) = send(obj.toString())
|
fun sendJson(obj: JsonObject) = send(obj.toString())
|
||||||
|
|
||||||
try {
|
|
||||||
sendJson(
|
sendJson(
|
||||||
buildJsonObject {
|
buildJsonObject {
|
||||||
put("type", JsonPrimitive("hello"))
|
put("type", JsonPrimitive("hello"))
|
||||||
@@ -111,6 +111,9 @@ class BridgePairingClient {
|
|||||||
}
|
}
|
||||||
else -> PairResult(ok = false, token = null, error = "unexpected bridge response")
|
else -> PairResult(ok = false, token = null, error = "unexpected bridge response")
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
val message = e.message?.trim().orEmpty().ifEmpty { "gateway unreachable" }
|
||||||
|
PairResult(ok = false, token = null, error = message)
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
socket.close()
|
socket.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user