From b820b6564f5812c28888398b93fdbd577e47f912 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 30 Jun 2023 08:58:14 -0400 Subject: [PATCH] BugFix Crashing relay screen on invalid URLs --- .../ui/actions/RelayInformationDialog.kt | 108 ++++++++++-------- 1 file changed, 60 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelayInformationDialog.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelayInformationDialog.kt index 1ca108c9a..eb26e9f49 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelayInformationDialog.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelayInformationDialog.kt @@ -292,32 +292,44 @@ fun loadRelayInfo( scope: CoroutineScope, onInfo: (RelayInformation) -> Unit ) { - val url = if (dirtyUrl.contains("://")) { - dirtyUrl - .replace("wss://", "https://") - .replace("ws://", "http://") - } else { - "https://$dirtyUrl" - } + try { + val url = if (dirtyUrl.contains("://")) { + dirtyUrl + .replace("wss://", "https://") + .replace("ws://", "http://") + } else { + "https://$dirtyUrl" + } - val request: Request = Request - .Builder() - .header("Accept", "application/nostr+json") - .url(url) - .build() + val request: Request = Request + .Builder() + .header("Accept", "application/nostr+json") + .url(url) + .build() - HttpClient.getHttpClient() - .newCall(request) - .enqueue( - object : Callback { - override fun onResponse(call: Call, response: Response) { - checkNotInMainThread() - response.use { - val body = it.body.string() - try { - if (it.isSuccessful) { - onInfo(RelayInformation.fromJson(body)) - } else { + HttpClient.getHttpClient() + .newCall(request) + .enqueue( + object : Callback { + override fun onResponse(call: Call, response: Response) { + checkNotInMainThread() + response.use { + val body = it.body.string() + try { + if (it.isSuccessful) { + onInfo(RelayInformation.fromJson(body)) + } else { + scope.launch { + Toast + .makeText( + context, + context.getString(R.string.an_error_ocurred_trying_to_get_relay_information, dirtyUrl), + Toast.LENGTH_SHORT + ).show() + } + } + } catch (e: Exception) { + Log.e("RelayInfoFail", "Resulting Message from Relay $dirtyUrl in not parseable: $body", e) scope.launch { Toast .makeText( @@ -327,31 +339,31 @@ fun loadRelayInfo( ).show() } } - } catch (e: Exception) { - Log.e("RelayInfoFail", "Resulting Message from Relay in not parseable: $body", e) - scope.launch { - Toast - .makeText( - context, - context.getString(R.string.an_error_ocurred_trying_to_get_relay_information, dirtyUrl), - Toast.LENGTH_SHORT - ).show() - } + } + } + + override fun onFailure(call: Call, e: IOException) { + Log.e("RelayInfoFail", "Resulting Message from Relay in not parseable $dirtyUrl", e) + scope.launch { + Toast + .makeText( + context, + context.getString(R.string.an_error_ocurred_trying_to_get_relay_information, dirtyUrl), + Toast.LENGTH_SHORT + ).show() } } } - - override fun onFailure(call: Call, e: IOException) { - Log.e("RelayInfoFail", "Resulting Message from Relay in not parseable", e) - scope.launch { - Toast - .makeText( - context, - context.getString(R.string.an_error_ocurred_trying_to_get_relay_information, dirtyUrl), - Toast.LENGTH_SHORT - ).show() - } - } - } - ) + ) + } catch (e: Exception) { + Log.e("RelayInfoFail", "Invalid URL $dirtyUrl", e) + scope.launch { + Toast + .makeText( + context, + context.getString(R.string.an_error_ocurred_trying_to_get_relay_information, dirtyUrl), + Toast.LENGTH_SHORT + ).show() + } + } }