diff --git a/src/main/kotlin/net/thauvin/erik/crypto/CryptoException.kt b/src/main/kotlin/net/thauvin/erik/crypto/CryptoException.kt index b5303df..1cd9dc7 100644 --- a/src/main/kotlin/net/thauvin/erik/crypto/CryptoException.kt +++ b/src/main/kotlin/net/thauvin/erik/crypto/CryptoException.kt @@ -37,6 +37,7 @@ package net.thauvin.erik.crypto */ class CryptoException @JvmOverloads constructor( var statusCode: Int = NO_STATUS, + var id: String, message: String, cause: Throwable? = null ) : Exception(message, cause) { diff --git a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt index f8c5b4b..0746f7d 100644 --- a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt +++ b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt @@ -73,9 +73,9 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe return CryptoPrice(getString("base"), getString("currency"), getString("amount").toBigDecimal()) } } catch (e: NumberFormatException) { - throw CryptoException(message = "Could not convert amount to number.", cause = e) + throw CryptoException(id = "convert_error", message = "Could not convert amount to number.", cause = e) } catch (e: JSONException) { - throw CryptoException(message = "Could not parse price data.", cause = e) + throw CryptoException(id = "parse_error", message = "Could not parse price data.", cause = e) } } @@ -101,17 +101,24 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe val request = Request.Builder().url(httpUrl).build() val response = client.newCall(request).execute() - val body = response.body?.string() ?: throw CryptoException(response.code, "Empty response.") + val body = response.body?.string() ?: throw CryptoException( + response.code, + id = "empty_response", + message = "Empty response." + ) try { val json = JSONObject(body) if (response.isSuccessful) { return body } else { val data = json.getJSONArray("errors") - throw CryptoException(response.code, data.getJSONObject(0).getString("message")) + throw CryptoException( + response.code, data.getJSONObject(0).getString("id"), + data.getJSONObject(0).getString("message") + ) } } catch (e: JSONException) { - throw CryptoException(response.code, "Could not parse data.", e) + throw CryptoException(response.code, id = "parse_error", "Could not parse data.", e) } } diff --git a/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt index cb27271..303b7fb 100644 --- a/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt +++ b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt @@ -134,6 +134,13 @@ class CryptoPriceTest { 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( message = "buyPrice(BTC,BAR)", exceptionClass = CryptoException::class, @@ -183,7 +190,11 @@ class CryptoPriceTest { assertEquals("A$12,345.60", aud.toCurrency(), "CryptoPrice(LTC,AUD)") val dk = CryptoPrice("BCH", "DKK", d) - assertEquals("12.345,60 kr.", dk.toCurrency(Locale("da", "DK")), "CryptoPrice(BCH,DKK)") + assertEquals( + "12.345,60 kr.", dk.toCurrency( + Locale.Builder().setLanguage("da").setRegion("DK").build() + ), "CryptoPrice(BCH,DKK)" + ) val jp = CryptoPrice("BTC", "JPY", d) assertEquals("¥12,345.60", jp.toCurrency(Locale.JAPAN), "CryptoPrice(BTC,JPY)")