mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-08-10 08:27:04 +00:00
fix(amethyst): report NIP-11 fetch failures as FAIL_TO_REACH_SERVER
This commit is contained in:
+11
-4
@@ -47,14 +47,21 @@ class Nip11Retriever(
|
||||
onError: (NormalizedRelayUrl, ErrorCode, String?) -> Unit,
|
||||
) = withContext(Dispatchers.IO) {
|
||||
val url = relay.toHttp()
|
||||
try {
|
||||
val request: Request =
|
||||
val request =
|
||||
try {
|
||||
Request
|
||||
.Builder()
|
||||
.header("Accept", "application/nostr+json")
|
||||
.url(url)
|
||||
.build()
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Log.e("RelayInfoFail", "Invalid URL ${relay.url}", e)
|
||||
onError(relay, ErrorCode.FAIL_TO_ASSEMBLE_URL, e.message)
|
||||
return@withContext
|
||||
}
|
||||
|
||||
try {
|
||||
val client = okHttpClient(relay)
|
||||
|
||||
client.newCall(request).executeAsync().use { response ->
|
||||
@@ -81,8 +88,8 @@ class Nip11Retriever(
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Log.e("RelayInfoFail", "Invalid URL ${relay.url}", e)
|
||||
onError(relay, ErrorCode.FAIL_TO_ASSEMBLE_URL, e.message)
|
||||
Log.e("RelayInfoFail", "Failed to fetch NIP-11 from ${relay.url}", e)
|
||||
onError(relay, ErrorCode.FAIL_TO_REACH_SERVER, e.message ?: e::class.simpleName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.model.nip11RelayInfo
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.fail
|
||||
import org.junit.Test
|
||||
import java.net.ConnectException
|
||||
|
||||
class Nip11RetrieverTest {
|
||||
@Test
|
||||
fun unreachableServerReportsFailToReachServer() =
|
||||
runBlocking {
|
||||
// Inject a fake client that simulates connection refused without building
|
||||
// a real OkHttpClient (which fails in amethyst JVM unit tests because
|
||||
// the Android OkHttp variant tries to detect Android via android.util.Log).
|
||||
val retriever =
|
||||
Nip11Retriever { _ ->
|
||||
throw ConnectException("Connection refused")
|
||||
}
|
||||
val relay = NormalizedRelayUrl("ws://127.0.0.1:14591/")
|
||||
|
||||
var errorCode: Nip11Retriever.ErrorCode? = null
|
||||
retriever.loadRelayInfo(
|
||||
relay = relay,
|
||||
onInfo = { fail("Expected an error, got relay info") },
|
||||
onError = { _, code, _ -> errorCode = code },
|
||||
)
|
||||
|
||||
assertEquals(Nip11Retriever.ErrorCode.FAIL_TO_REACH_SERVER, errorCode)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user