Added status code to exceptions.

This commit is contained in:
Erik C. Thauvin 2021-05-09 14:14:54 -07:00
parent f918aec230
commit 3ad86b3354
3 changed files with 32 additions and 14 deletions

View file

@ -34,11 +34,23 @@ package net.thauvin.erik.crypto
@Suppress("EmptySecondaryConstructor", "unused") @Suppress("EmptySecondaryConstructor", "unused")
class CryptoException : Exception { class CryptoException : Exception {
constructor(message: String, cause: Throwable) : super(message, cause) var statusCode = NO_STATUS
constructor(message: String) : super(message)
constructor(cause: Throwable) : super(cause) constructor(statusCode: Int = NO_STATUS, message: String, cause: Throwable) : super(message, cause) {
this.statusCode = statusCode
}
constructor(statusCode: Int = NO_STATUS, message: String) : super(message) {
this.statusCode = statusCode
}
constructor(statusCode: Int = NO_STATUS, cause: Throwable) : super(cause) {
this.statusCode = statusCode
}
companion object { companion object {
const val NO_STATUS = -1
private const val serialVersionUID = 1L private const val serialVersionUID = 1L
} }
} }

View file

@ -53,11 +53,11 @@ open class CryptoPrice(val base: String, val currency: String, val amount: Doubl
if (json.has("data")) { if (json.has("data")) {
with(json.getJSONObject("data")) { with(json.getJSONObject("data")) {
return CryptoPrice( return CryptoPrice(
getString("base"), getString("currency"), getString("amount").toDouble() getString("base"), getString("currency"), getString("amount").toDouble()
) )
} }
} else { } else {
throw CryptoException("Missing JSON data.") throw CryptoException(message = "Missing JSON data.")
} }
} }
@ -85,15 +85,15 @@ open class CryptoPrice(val base: String, val currency: String, val amount: Doubl
if (!response.isSuccessful) { if (!response.isSuccessful) {
if (json.has("errors")) { if (json.has("errors")) {
val data = json.getJSONArray("errors") val data = json.getJSONArray("errors")
throw CryptoException(data.getJSONObject(0).getString("message")) throw CryptoException(response.code, data.getJSONObject(0).getString("message"))
} else { } else {
throw CryptoException("Invalid API response. (${response.code}") throw CryptoException(response.code, "Invalid API response.")
} }
} else { } else {
return body return body
} }
} else { } else {
throw CryptoException("Empty API response.") throw CryptoException(response.code, "Empty API response.")
} }
} }

View file

@ -52,16 +52,22 @@ class CryptoPriceTest {
@Throws(CryptoException::class) @Throws(CryptoException::class)
fun testMarketPriceExceptions() { fun testMarketPriceExceptions() {
assertFailsWith( assertFailsWith(
message = "FOO did not fail", message = "FOO did not fail",
exceptionClass = CryptoException::class, exceptionClass = CryptoException::class,
block = { marketPrice("FOO") } block = { marketPrice("FOO") }
) )
assertFailsWith( assertFailsWith(
message = "BAR did not fail", message = "BAR did not fail",
exceptionClass = CryptoException::class, exceptionClass = CryptoException::class,
block = { marketPrice("BTC", "BAR") } block = { marketPrice("BTC", "BAR") }
) )
try {
marketPrice("FOOBAR")
} catch (e: CryptoException) {
assertTrue(e.statusCode != 400, "FOOBAR status code is not 400")
}
} }
@Test @Test