Improved error parsing

This commit is contained in:
Erik C. Thauvin 2023-09-23 10:52:35 -07:00
parent 7a658c20e0
commit 257cec56b4
2 changed files with 13 additions and 22 deletions

View file

@ -110,11 +110,16 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe
if (response.isSuccessful) { if (response.isSuccessful) {
return body return body
} else { } else {
val data = json.getJSONArray("errors") if (json.has("errors")) {
throw CryptoException( val data = json.getJSONArray("errors")
response.code, data.getJSONObject(0).getString("id"), throw CryptoException(
data.getJSONObject(0).getString("message") response.code, data.getJSONObject(0).getString("id"),
) data.getJSONObject(0).getString("message")
)
} else {
throw CryptoException(response.code, json.getString("error"),
json.getString("message"))
}
} }
} catch (e: JSONException) { } catch (e: JSONException) {
throw CryptoException(response.code, id = "parse_error", "Could not parse data.", e) throw CryptoException(response.code, id = "parse_error", "Could not parse data.", e)

View file

@ -33,10 +33,8 @@ package net.thauvin.erik.crypto
import assertk.all import assertk.all
import assertk.assertThat import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.isEqualTo import assertk.assertions.isEqualTo
import assertk.assertions.isGreaterThan import assertk.assertions.isGreaterThan
import assertk.assertions.isNotNull
import assertk.assertions.prop import assertk.assertions.prop
import net.thauvin.erik.crypto.CryptoPrice.Companion.apiCall import net.thauvin.erik.crypto.CryptoPrice.Companion.apiCall
import net.thauvin.erik.crypto.CryptoPrice.Companion.buyPrice import net.thauvin.erik.crypto.CryptoPrice.Companion.buyPrice
@ -126,20 +124,7 @@ class CryptoPriceTest {
@Test @Test
@Throws(CryptoException::class) @Throws(CryptoException::class)
fun testPrices() { fun testNotFound() {
assertFailsWith(
message = "spotPrice(FOO)",
exceptionClass = CryptoException::class,
block = { spotPrice("FOO") }
)
try {
spotPrice("BAR")
} catch (e: CryptoException) {
assertThat(e.id, "spotPrice(bar) error id").isEqualTo("not_found")
assertThat(e.message, "spotPrice(bar) error message").isEqualTo("Invalid base currency")
}
assertFailsWith( assertFailsWith(
message = "buyPrice(BTC,BAR)", message = "buyPrice(BTC,BAR)",
exceptionClass = CryptoException::class, exceptionClass = CryptoException::class,
@ -151,7 +136,8 @@ class CryptoPriceTest {
} catch (e: CryptoException) { } catch (e: CryptoException) {
assertThat(e, "sellPrice(FOOBAR)").all { assertThat(e, "sellPrice(FOOBAR)").all {
prop(CryptoException::statusCode).isEqualTo(404) prop(CryptoException::statusCode).isEqualTo(404)
prop(CryptoException::message).isNotNull().contains("invalid", true) prop(CryptoException::message).isEqualTo("not found")
prop(CryptoException::id).isEqualTo("not found")
} }
} }
} }