BugFix Crashing relay screen on invalid URLs

This commit is contained in:
Vitor Pamplona
2023-06-30 08:58:14 -04:00
parent 3908c42c7f
commit b820b6564f

View File

@@ -292,32 +292,44 @@ fun loadRelayInfo(
scope: CoroutineScope, scope: CoroutineScope,
onInfo: (RelayInformation) -> Unit onInfo: (RelayInformation) -> Unit
) { ) {
val url = if (dirtyUrl.contains("://")) { try {
dirtyUrl val url = if (dirtyUrl.contains("://")) {
.replace("wss://", "https://") dirtyUrl
.replace("ws://", "http://") .replace("wss://", "https://")
} else { .replace("ws://", "http://")
"https://$dirtyUrl" } else {
} "https://$dirtyUrl"
}
val request: Request = Request val request: Request = Request
.Builder() .Builder()
.header("Accept", "application/nostr+json") .header("Accept", "application/nostr+json")
.url(url) .url(url)
.build() .build()
HttpClient.getHttpClient() HttpClient.getHttpClient()
.newCall(request) .newCall(request)
.enqueue( .enqueue(
object : Callback { object : Callback {
override fun onResponse(call: Call, response: Response) { override fun onResponse(call: Call, response: Response) {
checkNotInMainThread() checkNotInMainThread()
response.use { response.use {
val body = it.body.string() val body = it.body.string()
try { try {
if (it.isSuccessful) { if (it.isSuccessful) {
onInfo(RelayInformation.fromJson(body)) onInfo(RelayInformation.fromJson(body))
} else { } 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 { scope.launch {
Toast Toast
.makeText( .makeText(
@@ -327,31 +339,31 @@ fun loadRelayInfo(
).show() ).show()
} }
} }
} catch (e: Exception) { }
Log.e("RelayInfoFail", "Resulting Message from Relay in not parseable: $body", e) }
scope.launch {
Toast override fun onFailure(call: Call, e: IOException) {
.makeText( Log.e("RelayInfoFail", "Resulting Message from Relay in not parseable $dirtyUrl", e)
context, scope.launch {
context.getString(R.string.an_error_ocurred_trying_to_get_relay_information, dirtyUrl), Toast
Toast.LENGTH_SHORT .makeText(
).show() 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) { } catch (e: Exception) {
Log.e("RelayInfoFail", "Resulting Message from Relay in not parseable", e) Log.e("RelayInfoFail", "Invalid URL $dirtyUrl", e)
scope.launch { scope.launch {
Toast Toast
.makeText( .makeText(
context, context,
context.getString(R.string.an_error_ocurred_trying_to_get_relay_information, dirtyUrl), context.getString(R.string.an_error_ocurred_trying_to_get_relay_information, dirtyUrl),
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
} }
} }
}
)
} }