From 3ad86b33545bde23d1cd58a06ccc381bc9373fdd Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 9 May 2021 14:14:54 -0700 Subject: [PATCH] Added status code to exceptions. --- .../net/thauvin/erik/crypto/CryptoException.kt | 18 +++++++++++++++--- .../net/thauvin/erik/crypto/CryptoPrice.kt | 10 +++++----- .../net/thauvin/erik/crypto/CryptoPriceTest.kt | 18 ++++++++++++------ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/crypto/CryptoException.kt b/src/main/kotlin/net/thauvin/erik/crypto/CryptoException.kt index 02e60c6..e9fa16f 100644 --- a/src/main/kotlin/net/thauvin/erik/crypto/CryptoException.kt +++ b/src/main/kotlin/net/thauvin/erik/crypto/CryptoException.kt @@ -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 } } diff --git a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt index b3cf1d6..20a76f3 100644 --- a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt +++ b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt @@ -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.") } } diff --git a/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt index 32a59de..abd23c6 100644 --- a/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt +++ b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt @@ -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