From a76a7b996ddfd10d114f2e3c32751940ba18a8bd Mon Sep 17 00:00:00 2001 From: gmantele <gmantele@ari.uni-heidelberg.de> Date: Mon, 13 Apr 2015 15:39:58 +0200 Subject: [PATCH] [UWS,TAP] Add the origin of the main exception after the exception class name in the log entries. This origin includes the class, the method, the file and the line where the exception has been thrown. --- src/tap/log/DefaultTAPLog.java | 4 +-- src/uws/service/log/DefaultUWSLog.java | 48 ++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/tap/log/DefaultTAPLog.java b/src/tap/log/DefaultTAPLog.java index 8374bed..90cbeba 100644 --- a/src/tap/log/DefaultTAPLog.java +++ b/src/tap/log/DefaultTAPLog.java @@ -96,12 +96,12 @@ public class DefaultTAPLog extends DefaultUWSLog implements TAPLog { if (error.getCause() != null) printException(error.getCause(), out); else{ - out.println("Caused by a " + error.getClass().getName()); + out.println("Caused by a " + error.getClass().getName() + " " + getExceptionOrigin(error)); if (error.getMessage() != null) out.println("\t" + error.getMessage()); } }else if (error instanceof SQLException){ - out.println("Caused by a " + error.getClass().getName()); + out.println("Caused by a " + error.getClass().getName() + " " + getExceptionOrigin(error)); out.print("\t"); do{ out.println(error.getMessage()); diff --git a/src/uws/service/log/DefaultUWSLog.java b/src/uws/service/log/DefaultUWSLog.java index a4e0129..637b022 100644 --- a/src/uws/service/log/DefaultUWSLog.java +++ b/src/uws/service/log/DefaultUWSLog.java @@ -346,11 +346,28 @@ public class DefaultUWSLog implements UWSLog { } /** - * Format and print the given exception inside the given writer. + * <p>Format and print the given exception inside the given writer.</p> + * + * <p>This function does nothing if the given error is NULL.</p> + * + * <p>The full stack trace is printed ONLY for unknown exceptions.</p> + * + * <p>The printed text has the following format for known exceptions:</p> + * <pre> + * Caused by a {ExceptionClassName} {ExceptionOrigin} + * {ExceptionMessage} + * </pre> + * + * <p>The printed text has the following format for unknown exceptions:</p> + * <pre> + * Caused by a {ExceptionFullStackTrace} + * </pre> * * @param error The exception to print. * @param out The output in which the exception must be written. * + * @see #getExceptionOrigin(Throwable) + * * @since 4.1 */ protected void printException(final Throwable error, final PrintWriter out){ @@ -359,7 +376,7 @@ public class DefaultUWSLog implements UWSLog { if (error.getCause() != null) printException(error.getCause(), out); else{ - out.println("Caused by a " + error.getClass().getName()); + out.println("Caused by a " + error.getClass().getName() + " " + getExceptionOrigin(error)); if (error.getMessage() != null) out.println("\t" + error.getMessage()); } @@ -370,6 +387,33 @@ public class DefaultUWSLog implements UWSLog { } } + /** + * <p>Format and return the origin of the given error. + * "Origin" means here: "where the error has been thrown from?" (from which class? method? file? line?).</p> + * + * <p>This function does nothing if the given error is NULL or if the origin information is missing.</p> + * + * <p>The returned text has the following format:</p> + * <pre> + * at {OriginClass}.{OriginMethod}({OriginFile}:{OriginLine}) + * </pre> + * + * <p>{OriginFile} and {OriginLine} are written only if provided.</p> + * + * @param error Error whose the origin should be returned. + * + * @return A string which contains formatted information about the origin of the given error. + * + * @since 4.1 + */ + protected String getExceptionOrigin(final Throwable error){ + if (error != null && error.getStackTrace() != null && error.getStackTrace().length > 0){ + StackTraceElement src = error.getStackTrace()[0]; + return "at " + src.getClassName() + "." + src.getMethodName() + ((src.getFileName() != null) ? "(" + src.getFileName() + ((src.getLineNumber() >= 0) ? ":" + src.getLineNumber() : "") + ")" : ""); + }else + return ""; + } + @Override public void debug(String msg){ log(LogLevel.DEBUG, null, msg, null); -- GitLab