From 0f6af785a5db8fc377c3784a8895a618dd7b633c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 25 Aug 2018 08:20:33 +0100 Subject: [PATCH] Don't throw generic exceptions --- .../assertions/MimeMessageException.java | 39 ++++++++++++++++++ .../assertions/WiserAssertionException.java | 40 +++++++++++++++++++ .../wiser/assertions/WiserAssertions.java | 8 ++-- 3 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/kemitix/wiser/assertions/MimeMessageException.java create mode 100644 src/main/java/net/kemitix/wiser/assertions/WiserAssertionException.java diff --git a/src/main/java/net/kemitix/wiser/assertions/MimeMessageException.java b/src/main/java/net/kemitix/wiser/assertions/MimeMessageException.java new file mode 100644 index 0000000..e407812 --- /dev/null +++ b/src/main/java/net/kemitix/wiser/assertions/MimeMessageException.java @@ -0,0 +1,39 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2018 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.wiser.assertions; + +/** + * Assertions caused by errors while parsing mime message content. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class MimeMessageException extends RuntimeException { + + /** + * Create an assertion with a message. + * + * @param message the message + */ + public MimeMessageException(final String message) { + super(message); + } +} diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertionException.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertionException.java new file mode 100644 index 0000000..59053e6 --- /dev/null +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertionException.java @@ -0,0 +1,40 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2018 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.wiser.assertions; + +/** + * Assertions caused by {@link WiserAssertions}. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class WiserAssertionException extends RuntimeException { + + /** + * Create a wrapper for the cause. + * + * @param cause the original exception + */ + public WiserAssertionException(final Throwable cause) { + super(cause); + } + +} diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index 1d3b26a..e8aa8d1 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -187,12 +187,12 @@ public final class WiserAssertions { * * @return the product of the supplier */ - @SuppressWarnings("illegalCatch") + @SuppressWarnings("illegalcatch") public static T unchecked(final ThrowingSupplier supplier) { try { return supplier.get(); } catch (Throwable e) { - throw new RuntimeException(e); + throw new WiserAssertionException(e); } } @@ -275,7 +275,7 @@ public final class WiserAssertions { @SuppressWarnings("npathcomplexity") private String getMimeMessageBody(final WiserMessage message) throws IOException, MessagingException { - Object content = getMimeMessage(message).getContent(); + final Object content = getMimeMessage(message).getContent(); String result = null; if (content instanceof String) { result = (String) content; @@ -287,7 +287,7 @@ public final class WiserAssertions { result = getMimeMultipartAsString((MimeMultipart) content); } if (result == null) { - throw new RuntimeException("Unexpected MimeMessage content"); + throw new MimeMessageException("Unexpected MimeMessage content"); } return result; }