[New] Added new ExceptionHelper

- moved exception helper methods from StringHelper to ExceptionHelper
This commit is contained in:
Robert von Burg 2015-07-15 10:50:12 +02:00
parent 2b9d09632c
commit 2d922df572
2 changed files with 93 additions and 48 deletions

View File

@ -0,0 +1,87 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* 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 <eitch@eitchnet.ch>
*/
public class ExceptionHelper {
/**
* <p>
* Returns a message for the given {@link Throwable}
* </p>
*
* <p>
* A {@link NullPointerException} only has <code>null</code> as the message so this methods returns the class name
* in such a case
* </p>
*
* @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());
}
}

View File

@ -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 {
}
/**
* <p>
* Returns a message for the given {@link Throwable}
* </p>
*
* <p>
* A {@link NullPointerException} only has <code>null</code> as the message so this methods returns the class name
* in such a case
* </p>
*
* @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);
}
/**