From 607a1b043967d22eb2afda4acc7069909ae783fb Mon Sep 17 00:00:00 2001 From: M57 <41645758+Hy4ri@users.noreply.github.com> Date: Fri, 19 Jun 2026 21:09:19 +0300 Subject: [PATCH] fix(#73): surface clear errors for malformed pairing strings instead of silent fallback Co-authored-by: M57 --- .gitignore | 1 + .../ui/connect/ConnectViewModel.kt | 31 +++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index e86d61cc..0f040ac1 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ local.properties \$PWD/ $PWD/ +ktlint diff --git a/app/src/main/java/com/m57/hermescontrol/ui/connect/ConnectViewModel.kt b/app/src/main/java/com/m57/hermescontrol/ui/connect/ConnectViewModel.kt index 0c20ea89..1888bb83 100644 --- a/app/src/main/java/com/m57/hermescontrol/ui/connect/ConnectViewModel.kt +++ b/app/src/main/java/com/m57/hermescontrol/ui/connect/ConnectViewModel.kt @@ -165,12 +165,37 @@ class ConnectViewModel : ViewModel() { connect() return } + // Decoded valid JSON but missing required fields + _uiState.update { + it.copy( + errorMessage = "Malformed pairing string — missing host, port, or token", + ) + } + return } + // Decoded to valid string but not JSON shape — not a pairing string + _uiState.update { + it.copy( + errorMessage = "Malformed pairing string — expected URL or Base64-encoded JSON", + ) + } + return + } catch (e: IllegalArgumentException) { + // Not valid Base64 — check if input looks like a raw token + if (trimmed.length >= 32 && trimmed.matches(Regex("[A-Za-z0-9_\\-]+"))) { + _uiState.update { it.copy(token = trimmed, errorMessage = null) } + } else { + _uiState.update { + it.copy( + errorMessage = "Malformed pairing string — expected URL or Base64-encoded JSON", + ) + } + } + return } catch (e: Exception) { - // Ignore base64 decode failures, fallback to setting raw token + _uiState.update { it.copy(errorMessage = "Failed to parse pairing string: ${e.message}") } + return } - - _uiState.update { it.copy(token = trimmed, errorMessage = null) } } catch (e: Exception) { _uiState.update { it.copy(errorMessage = "Failed to parse pairing string: ${e.message}") } }