Added id to CryptoException
This commit is contained in:
parent
ee74cf0cc4
commit
cb8bf44768
3 changed files with 25 additions and 6 deletions
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue