Reworked ModuleException.

This commit is contained in:
Erik C. Thauvin 2021-11-14 21:56:06 -08:00
parent 29eae2e7ee
commit 3ea0e4b3ac
11 changed files with 58 additions and 49 deletions

View file

@ -35,6 +35,7 @@
<ID>MagicNumber:View.kt$View$6</ID>
<ID>MagicNumber:Weather2.kt$Weather2.Companion$1.60934</ID>
<ID>MagicNumber:Weather2.kt$Weather2.Companion$32</ID>
<ID>MagicNumber:Weather2.kt$Weather2.Companion$404</ID>
<ID>MagicNumber:Weather2.kt$Weather2.Companion$5</ID>
<ID>MagicNumber:Weather2.kt$Weather2.Companion$9</ID>
<ID>MagicNumber:WorldTime.kt$WorldTime$14</ID>

View file

@ -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
)

View file

@ -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<Message> {
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<Message>()
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."))

View file

@ -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)
}
}
}

View file

@ -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
}

View file

@ -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<Message> {
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))

View file

@ -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)
}
}
}

View file

@ -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<Message> {
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<Message>()
@ -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)
}
}
}

View file

@ -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)

View file

@ -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")

View file

@ -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