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")
|
@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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ open class CryptoPrice(val base: String, val currency: String, val amount: Doubl
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} 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.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,6 +62,12 @@ class CryptoPriceTest {
|
||||||
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue