From 3ea0e4b3ac69e17e3c759359607e47a4bd1e56dd Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 14 Nov 2021 21:56:06 -0800 Subject: [PATCH] Reworked ModuleException. --- config/detekt/baseline.xml | 1 + .../erik/mobibot/modules/CurrencyConverter.kt | 4 +-- .../erik/mobibot/modules/GoogleSearch.kt | 17 +++++++--- .../net/thauvin/erik/mobibot/modules/Joke.kt | 8 +++-- .../erik/mobibot/modules/ModuleException.kt | 32 +++---------------- .../erik/mobibot/modules/StockQuote.kt | 9 ++++-- .../thauvin/erik/mobibot/modules/Twitter.kt | 4 ++- .../thauvin/erik/mobibot/modules/Weather2.kt | 21 +++++++++--- .../erik/mobibot/msg/PrivateMessage.kt | 1 + .../mobibot/modules/ModuleExceptionTest.kt | 4 +-- version.properties | 6 ++-- 11 files changed, 58 insertions(+), 49 deletions(-) diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index 9bc0b96..3bcc3cb 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -35,6 +35,7 @@ MagicNumber:View.kt$View$6 MagicNumber:Weather2.kt$Weather2.Companion$1.60934 MagicNumber:Weather2.kt$Weather2.Companion$32 + MagicNumber:Weather2.kt$Weather2.Companion$404 MagicNumber:Weather2.kt$Weather2.Companion$5 MagicNumber:Weather2.kt$Weather2.Companion$9 MagicNumber:WorldTime.kt$WorldTime$14 diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt index cd4bb67..ebaadb8 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt @@ -210,13 +210,13 @@ class CurrencyConverter : ThreadedModule() { EXCHANGE_RATES["EUR"] = "1" } catch (e: JDOMException) { throw ModuleException( - e.message, + "loadRates(): JDOM", "An JDOM parsing error has occurred while parsing the exchange rates table.", e ) } catch (e: IOException) { throw ModuleException( - e.message, + "loadRates(): IOE", "An IO error has occurred while parsing the exchange rates table.", e ) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt index 9571556..efe52ec 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt @@ -70,7 +70,9 @@ class GoogleSearch : ThreadedModule() { } } catch (e: ModuleException) { if (logger.isWarnEnabled) logger.warn(e.debugMessage, e) - event.sendMessage(e.message!!) + e.message?.let { + event.sendMessage(it) + } } } else { helpResponse(event) @@ -94,7 +96,10 @@ class GoogleSearch : ThreadedModule() { @Throws(ModuleException::class) fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List { if (apiKey.isNullOrBlank() || cseKey.isNullOrBlank()) { - throw ModuleException("${GOOGLE_CMD.capitalise()} is disabled. The API keys are missing.") + throw ModuleException( + "${GoogleSearch::class.java.name} is disabled.", + "${GOOGLE_CMD.capitalise()} is disabled. The API keys are missing." + ) } val results = mutableListOf() if (query.isNotBlank()) { @@ -115,9 +120,13 @@ class GoogleSearch : ThreadedModule() { results.add(ErrorMessage("No results found.", Colors.RED)) } } catch (e: IOException) { - throw ModuleException("searchGoogle($query)", "An IO error has occurred searching Google.", e) + throw ModuleException("searchGoogle($query): IOE", "An IO error has occurred searching Google.", e) } catch (e: JSONException) { - throw ModuleException("searchGoogle($query)", "A JSON error has occurred searching Google.", e) + throw ModuleException( + "searchGoogle($query): JSON", + "A JSON error has occurred searching Google.", + e + ) } } else { results.add(ErrorMessage("Invalid query. Please try again.")) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt index 2d5af61..3ba5f00 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt @@ -69,7 +69,9 @@ class Joke : ThreadedModule() { sendIRC().notice(channel, cyan(randomJoke().msg)) } catch (e: ModuleException) { if (logger.isWarnEnabled) logger.warn(e.debugMessage, e) - event.sendMessage(e.message!!) + e.message?.let { + event.sendMessage(it) + } } } } @@ -96,9 +98,9 @@ class Joke : ThreadedModule() { .replace("\\\"", "\"") ) } catch (e: IOException) { - throw ModuleException("randomJoke()", "An IO error has occurred retrieving a random joke.", e) + throw ModuleException("randomJoke(): IOE", "An IO error has occurred retrieving a random joke.", e) } catch (e: JSONException) { - throw ModuleException("randomJoke()", "An JSON error has occurred retrieving a random joke.", e) + throw ModuleException("randomJoke(): JSON", "An JSON error has occurred retrieving a random joke.", e) } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ModuleException.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ModuleException.kt index 4c6867a..cf06b4e 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ModuleException.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ModuleException.kt @@ -34,33 +34,11 @@ package net.thauvin.erik.mobibot.modules /** * The `ModuleException` class. */ -class ModuleException : Exception { - /** - * Returns the debug message. - */ - val debugMessage: String? - - /** - * Creates a new exception. - */ - constructor(message: String?) : super(message) { - debugMessage = message - } - - /** - * Creates a new exception. - */ - constructor(debugMessage: String?, message: String?, cause: Throwable?) : super(message, cause) { - this.debugMessage = debugMessage - } - - /** - * Creates a new exception. - */ - constructor(debugMessage: String?, message: String?) : super(message) { - this.debugMessage = debugMessage - } - +class ModuleException @JvmOverloads constructor( + val debugMessage: String, + message: String? = null, + cause: Throwable? = null +) : Exception(message, cause) { companion object { private const val serialVersionUID = 1L } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt index 230e2af..a1f17c4 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt @@ -67,7 +67,9 @@ class StockQuote : ThreadedModule() { } } catch (e: ModuleException) { if (logger.isWarnEnabled) logger.warn(e.debugMessage, e) - event.sendMessage(e.message!!) + e.message?.let { + event.sendMessage(it) + } } } else { helpResponse(event) @@ -129,6 +131,7 @@ class StockQuote : ThreadedModule() { fun getQuote(symbol: String, apiKey: String?): List { if (apiKey.isNullOrBlank()) { throw ModuleException( + "${StockQuote::class.java.name} is disabled.", "${STOCK_CMD.capitalise()} is disabled. The API key is missing." ) } @@ -216,9 +219,9 @@ class StockQuote : ThreadedModule() { } } } catch (e: IOException) { - throw ModuleException(debugMessage, "An IO error has occurred retrieving a stock quote.", e) + throw ModuleException("$debugMessage: IOE", "An IO error has occurred retrieving a stock quote.", e) } catch (e: NullPointerException) { - throw ModuleException(debugMessage, "An error has occurred retrieving a stock quote.", e) + throw ModuleException("$debugMessage: NPE", "An error has occurred retrieving a stock quote.", e) } } else { messages.add(ErrorMessage(INVALID_SYMBOL)) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt index f6020e3..c813392 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt @@ -167,7 +167,9 @@ class Twitter : ThreadedModule() { event.respond(post(event.user.nick, "$args (by ${event.user.nick} on $channel)", false)) } catch (e: ModuleException) { if (logger.isWarnEnabled) logger.warn(e.debugMessage, e) - event.respond(e.message) + e.message?.let { + event.respond(it) + } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt index 246c52a..273c374 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt @@ -73,7 +73,9 @@ class Weather2 : ThreadedModule() { } } catch (e: ModuleException) { if (logger.isWarnEnabled) logger.warn(e.debugMessage, e) - event.respond(e.message) + e.message?.let { + event.respond(it) + } } } else { helpResponse(event) @@ -116,7 +118,10 @@ class Weather2 : ThreadedModule() { @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List { if (apiKey.isNullOrBlank()) { - throw ModuleException("${WEATHER_CMD.capitalise()} is disabled. The API key is missing.") + throw ModuleException( + "${Weather2::class.java.name} is disabled.", + "${WEATHER_CMD.capitalise()} is disabled. The API key is missing." + ) } val owm = OWM(apiKey) val messages = mutableListOf() @@ -199,9 +204,17 @@ class Weather2 : ThreadedModule() { } } } catch (e: APIException) { - throw ModuleException("getWeather($query)", e.message, e) + if (e.code == 404) { + throw ModuleException( + "getWeather($query): API ${e.code}", + "The requested city was not found.", + e + ) + } else { + throw ModuleException("getWeather($query): API ${e.code}", e.message, e) + } } catch (e: NullPointerException) { - throw ModuleException("getWeather($query)", "Unable to perform weather lookup.", e) + throw ModuleException("getWeather($query): NPE", "Unable to perform weather lookup.", e) } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/msg/PrivateMessage.kt b/src/main/kotlin/net/thauvin/erik/mobibot/msg/PrivateMessage.kt index 7515eae..6edcb96 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/msg/PrivateMessage.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/msg/PrivateMessage.kt @@ -34,5 +34,6 @@ package net.thauvin.erik.mobibot.msg /** * The `PrivateMessage` class. */ +@Suppress("unused") class PrivateMessage @JvmOverloads constructor(msg: String, color: String = DEFAULT_COLOR) : Message(msg, color, isPrivate = true) diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt index d776f2c..5791867 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt @@ -89,14 +89,14 @@ class ModuleExceptionTest { e = ModuleException(debugMessage, message, IOException()) assertThat(e.sanitize(apiKey), "no cause message").hasMessage(message) - e = ModuleException(apiKey) + e = ModuleException(debugMessage, apiKey) assertThat(e.sanitize(apiKey).message, "api key in message").isNotNull().doesNotContain(apiKey) val msg: String? = null e = ModuleException(debugMessage, msg, IOException(msg)) assertThat(e.sanitize(apiKey).message, "null message").isNull() - e = ModuleException(msg, msg, IOException("foo is $apiKey")) + e = ModuleException(debugMessage, msg, IOException("foo is $apiKey")) assertThat(e.sanitize(" ", apiKey, "foo").message, "key in cause").isNotNull().all { doesNotContain(apiKey) endsWith("xxx is xxxxxxxxxx") diff --git a/version.properties b/version.properties index c002730..d3947a4 100644 --- a/version.properties +++ b/version.properties @@ -1,9 +1,9 @@ #Generated by the Semver Plugin for Gradle -#Tue Nov 09 08:47:23 PST 2021 -version.buildmeta=2227 +#Sun Nov 14 21:44:14 PST 2021 +version.buildmeta=2262 version.major=0 version.minor=8 version.patch=0 version.prerelease=beta version.project=mobibot -version.semver=0.8.0-beta+2227 +version.semver=0.8.0-beta+2262