From 2d922df5724ca10d521e768df65d389677b28af6 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 15 Jul 2015 10:50:12 +0200 Subject: [PATCH] [New] Added new ExceptionHelper - moved exception helper methods from StringHelper to ExceptionHelper --- .../utils/helper/ExceptionHelper.java | 87 +++++++++++++++++++ .../eitchnet/utils/helper/StringHelper.java | 54 ++---------- 2 files changed, 93 insertions(+), 48 deletions(-) create mode 100644 src/main/java/ch/eitchnet/utils/helper/ExceptionHelper.java diff --git a/src/main/java/ch/eitchnet/utils/helper/ExceptionHelper.java b/src/main/java/ch/eitchnet/utils/helper/ExceptionHelper.java new file mode 100644 index 000000000..cfe5166dd --- /dev/null +++ b/src/main/java/ch/eitchnet/utils/helper/ExceptionHelper.java @@ -0,0 +1,87 @@ +/* + * Copyright 2015 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ch.eitchnet.utils.helper; + +import java.io.PrintWriter; +import java.io.StringWriter; + +/** + * @author Robert von Burg + */ +public class ExceptionHelper { + + /** + *

+ * Returns a message for the given {@link Throwable} + *

+ * + *

+ * A {@link NullPointerException} only has null as the message so this methods returns the class name + * in such a case + *

+ * + * @param t + * @return + */ + public static String getExceptionMessage(Throwable t) { + return StringHelper.isEmpty(t.getMessage()) ? t.getClass().getName() : t.getMessage(); + } + + /** + * Formats the given {@link Throwable}'s stack trace to a string + * + * @param t + * the throwable for which the stack trace is to be formatted to string + * + * @return a string representation of the given {@link Throwable}'s stack trace + */ + public static String formatException(Throwable t) { + StringWriter stringWriter = new StringWriter(); + PrintWriter writer = new PrintWriter(stringWriter); + t.printStackTrace(writer); + return stringWriter.toString(); + } + + /** + * Formats the given {@link Throwable}'s message including causes to a string + * + * @param t + * the throwable for which the messages are to be formatted to a string + * + * @return a string representation of the given {@link Throwable}'s messages including causes + */ + public static String formatExceptionMessage(Throwable t) { + StringBuilder sb = new StringBuilder(); + sb.append(t.getMessage()); + appendCause(sb, t); + return sb.toString(); + } + + private static void appendCause(StringBuilder sb, Throwable e) { + Throwable cause = e.getCause(); + if (cause == null) + return; + + sb.append("\n"); + + sb.append("cause:\n"); + sb.append(cause.getMessage()); + + if (cause.getCause() != null) + appendCause(sb, cause.getCause()); + } + +} diff --git a/src/main/java/ch/eitchnet/utils/helper/StringHelper.java b/src/main/java/ch/eitchnet/utils/helper/StringHelper.java index b0adc8e75..ecd20775c 100644 --- a/src/main/java/ch/eitchnet/utils/helper/StringHelper.java +++ b/src/main/java/ch/eitchnet/utils/helper/StringHelper.java @@ -15,8 +15,6 @@ */ package ch.eitchnet.utils.helper; -import java.io.PrintWriter; -import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -564,64 +562,24 @@ public class StringHelper { } /** - *

- * Returns a message for the given {@link Throwable} - *

- * - *

- * A {@link NullPointerException} only has null as the message so this methods returns the class name - * in such a case - *

- * - * @param t - * @return + * @see ExceptionHelper#formatException(Throwable) */ public static String getExceptionMessage(Throwable t) { - return StringHelper.isEmpty(t.getMessage()) ? t.getClass().getName() : t.getMessage(); + return ExceptionHelper.getExceptionMessage(t); } /** - * Formats the given {@link Throwable}'s stack trace to a string - * - * @param t - * the throwable for which the stack trace is to be formatted to string - * - * @return a string representation of the given {@link Throwable}'s stack trace + * @see ExceptionHelper#formatException(Throwable) */ public static String formatException(Throwable t) { - StringWriter stringWriter = new StringWriter(); - PrintWriter writer = new PrintWriter(stringWriter); - t.printStackTrace(writer); - return stringWriter.toString(); + return ExceptionHelper.formatException(t); } /** - * Formats the given {@link Throwable}'s message including causes to a string - * - * @param t - * the throwable for which the messages are to be formatted to a string - * - * @return a string representation of the given {@link Throwable}'s messages including causes + * @see ExceptionHelper#formatExceptionMessage(Throwable) */ public static String formatExceptionMessage(Throwable t) { - StringBuilder sb = new StringBuilder(); - sb.append(t.getMessage()); - appendCause(sb, t); - return sb.toString(); - } - - private static void appendCause(StringBuilder sb, Throwable e) { - Throwable cause = e.getCause(); - if (cause == null) - return; - - sb.append("\n"); - - sb.append("cause:\n"); - sb.append(cause.getMessage()); - - if (cause.getCause() != null) - appendCause(sb, cause.getCause()); + return ExceptionHelper.formatExceptionMessage(t); } /**