Added status code to exceptions.
This commit is contained in:
parent
f918aec230
commit
3ad86b3354
3 changed files with 32 additions and 14 deletions
|
@ -34,11 +34,23 @@ package net.thauvin.erik.crypto
|
|||
|
||||
@Suppress("EmptySecondaryConstructor", "unused")
|
||||
class CryptoException : Exception {
|
||||
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||
constructor(message: String) : super(message)
|
||||
constructor(cause: Throwable) : super(cause)
|
||||
var statusCode = NO_STATUS
|
||||
|
||||
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 {
|
||||
const val NO_STATUS = -1
|
||||
|
||||
private const val serialVersionUID = 1L
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,11 +53,11 @@ open class CryptoPrice(val base: String, val currency: String, val amount: Doubl
|
|||
if (json.has("data")) {
|
||||
with(json.getJSONObject("data")) {
|
||||
return CryptoPrice(
|
||||
getString("base"), getString("currency"), getString("amount").toDouble()
|
||||
getString("base"), getString("currency"), getString("amount").toDouble()
|
||||
)
|
||||
}
|
||||
} 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 (json.has("errors")) {
|
||||
val data = json.getJSONArray("errors")
|
||||
throw CryptoException(data.getJSONObject(0).getString("message"))
|
||||
throw CryptoException(response.code, data.getJSONObject(0).getString("message"))
|
||||
} else {
|
||||
throw CryptoException("Invalid API response. (${response.code}")
|
||||
throw CryptoException(response.code, "Invalid API response.")
|
||||
}
|
||||
} else {
|
||||
return body
|
||||
}
|
||||
} else {
|
||||
throw CryptoException("Empty API response.")
|
||||
throw CryptoException(response.code, "Empty API response.")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,16 +52,22 @@ class CryptoPriceTest {
|
|||
@Throws(CryptoException::class)
|
||||
fun testMarketPriceExceptions() {
|
||||
assertFailsWith(
|
||||
message = "FOO did not fail",
|
||||
exceptionClass = CryptoException::class,
|
||||
block = { marketPrice("FOO") }
|
||||
message = "FOO did not fail",
|
||||
exceptionClass = CryptoException::class,
|
||||
block = { marketPrice("FOO") }
|
||||
)
|
||||
|
||||
assertFailsWith(
|
||||
message = "BAR did not fail",
|
||||
exceptionClass = CryptoException::class,
|
||||
block = { marketPrice("BTC", "BAR") }
|
||||
message = "BAR did not fail",
|
||||
exceptionClass = CryptoException::class,
|
||||
block = { marketPrice("BTC", "BAR") }
|
||||
)
|
||||
|
||||
try {
|
||||
marketPrice("FOOBAR")
|
||||
} catch (e: CryptoException) {
|
||||
assertTrue(e.statusCode != 400, "FOOBAR status code is not 400")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue