Add string formatting functionality to ExceptionWithContext

This commit is contained in:
Ben Gruver 2012-10-15 22:44:15 -07:00
parent 3019737ed3
commit e8665c7a91

View File

@ -45,7 +45,7 @@ public class ExceptionWithContext
* @param str non-null; context to add * @param str non-null; context to add
* @return non-null; an appropriate instance * @return non-null; an appropriate instance
*/ */
public static ExceptionWithContext withContext(Throwable ex, String str) { public static ExceptionWithContext withContext(Throwable ex, String str, Object... formatArgs) {
ExceptionWithContext ewc; ExceptionWithContext ewc;
if (ex instanceof ExceptionWithContext) { if (ex instanceof ExceptionWithContext) {
@ -54,7 +54,7 @@ public class ExceptionWithContext
ewc = new ExceptionWithContext(ex); ewc = new ExceptionWithContext(ex);
} }
ewc.addContext(str); ewc.addContext(String.format(str, formatArgs));
return ewc; return ewc;
} }
@ -63,8 +63,8 @@ public class ExceptionWithContext
* *
* @param message human-oriented message * @param message human-oriented message
*/ */
public ExceptionWithContext(String message) { public ExceptionWithContext(String message, Object... formatArgs) {
this(message, null); this(null, message, formatArgs);
} }
/** /**
@ -73,7 +73,7 @@ public class ExceptionWithContext
* @param cause null-ok; exception that caused this one * @param cause null-ok; exception that caused this one
*/ */
public ExceptionWithContext(Throwable cause) { public ExceptionWithContext(Throwable cause) {
this(null, cause); this(cause, null);
} }
/** /**
@ -82,8 +82,8 @@ public class ExceptionWithContext
* @param message human-oriented message * @param message human-oriented message
* @param cause null-ok; exception that caused this one * @param cause null-ok; exception that caused this one
*/ */
public ExceptionWithContext(String message, Throwable cause) { public ExceptionWithContext(Throwable cause, String message, Object... formatArgs) {
super((message != null) ? message : super((message != null) ? formatMessage(message, formatArgs) :
(cause != null) ? cause.getMessage() : null, (cause != null) ? cause.getMessage() : null,
cause); cause);
@ -96,6 +96,13 @@ public class ExceptionWithContext
} }
} }
private static String formatMessage(String message, Object... formatArgs) {
if (message == null) {
return null;
}
return String.format(message, formatArgs);
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void printStackTrace(PrintStream out) { public void printStackTrace(PrintStream out) {