diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java
index 3cd623c..35019b2 100644
--- a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java
+++ b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java
@@ -32,7 +32,8 @@
package net.thauvin.erik.mobibot.modules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import net.thauvin.erik.mobibot.*;
+import net.thauvin.erik.mobibot.Mobibot;
+import net.thauvin.erik.mobibot.Utils;
import net.thauvin.erik.mobibot.msg.ErrorMessage;
import net.thauvin.erik.mobibot.msg.Message;
import net.thauvin.erik.mobibot.msg.PrivateMessage;
@@ -83,7 +84,7 @@ public final class CurrencyConverter extends AbstractModule {
commands.add(CURRENCY_CMD);
}
- static Message converyCurrency(String query) {
+ static Message converyCurrency(String query) throws ModuleException {
if (EXCHANGE_RATES.isEmpty()) {
try {
final SAXBuilder builder = new SAXBuilder();
@@ -109,10 +110,10 @@ public final class CurrencyConverter extends AbstractModule {
EXCHANGE_RATES.put("EUR", "1");
} catch (JDOMException e) {
- return new ErrorMessage("An error has occurred while parsing the exchange rates table.");
+ throw new ModuleException(query, "An error has occurred while parsing the exchange rates table.", e);
} catch (IOException e) {
- return new ErrorMessage(
- "An error has occurred while fetching the exchange rates table: " + e.getMessage());
+ throw new ModuleException(
+ query, "An error has occurred while fetching the exchange rates table.", e);
}
}
@@ -199,11 +200,16 @@ public final class CurrencyConverter extends AbstractModule {
private void run(final Mobibot bot, final String sender, final String query) {
if (Utils.isValidString(sender) && Utils.isValidString(query)) {
if (query.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-zA-Z]{3}+ to [a-zA-Z]{3}+")) {
- final Message msg = converyCurrency(query.substring(query.indexOf(' ')));
- if (msg.isError()) {
- helpResponse(bot, sender, CURRENCY_CMD + ' ' + query, true);
+ try {
+ final Message msg = converyCurrency(query.substring(query.indexOf(' ')));
+ if (msg.isError()) {
+ helpResponse(bot, sender, CURRENCY_CMD + ' ' + query, true);
+ }
+ bot.send(msg.isPrivate() ? sender : bot.getChannel(), msg.getMessage());
+ } catch (ModuleException e) {
+ bot.getLogger().debug(e.getMessage(), e);
+ bot.send(sender, e.getMessage());
}
- bot.send(msg.isPrivate() ? sender : bot.getChannel(), msg.getMessage());
} else {
helpResponse(bot, sender, CURRENCY_CMD + ' ' + query, true);
}
diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.java b/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.java
new file mode 100644
index 0000000..68ba44b
--- /dev/null
+++ b/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.java
@@ -0,0 +1,75 @@
+/*
+ * ModuleException.java
+ *
+ * Copyright (c) 2004-2019, Erik C. Thauvin (erik@thauvin.net)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * Neither the name of this project nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package net.thauvin.erik.mobibot.modules;
+
+/**
+ * The ModuleExcepetion
class.
+ *
+ * @author Erik C. Thauvin
+ * @created 2019-04-07
+ * @since 1.0
+ */
+public class ModuleException extends Exception {
+ private static final long serialVersionUID = -3036774290621088107L;
+
+ private final String query;
+
+ ModuleException(String query, String message) {
+ super(message);
+ this.query = query;
+
+ }
+
+ ModuleException(String query, Throwable cause) {
+ super(cause);
+ this.query = query;
+ }
+
+ ModuleException(String query, String message, Throwable cause) {
+ super(message, cause);
+ this.query = query;
+ }
+
+ ModuleException(String message) {
+ super(message);
+ query = "";
+ }
+
+ ModuleException(Throwable cause) {
+ super(cause);
+ query = "";
+ }
+
+ public String getQuery() {
+ return query;
+ }
+}
diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/CurrencyConvertTest.java b/src/test/java/net/thauvin/erik/mobibot/modules/CurrencyConvertTest.java
index 9c9238a..cf5a6c7 100644
--- a/src/test/java/net/thauvin/erik/mobibot/modules/CurrencyConvertTest.java
+++ b/src/test/java/net/thauvin/erik/mobibot/modules/CurrencyConvertTest.java
@@ -49,7 +49,7 @@ public class CurrencyConvertTest {
}
@Test
- public void testConvertCurrency() {
+ public void testConvertCurrency() throws ModuleException {
assertThat(CurrencyConverter.converyCurrency("100 USD to EUR").getMessage())
.as("100 USD to EUR").startsWith("100.00 USD = ");
assertThat(CurrencyConverter.converyCurrency("100 BLA to USD").isError())