PropagatedRuntimeException logs (at trace) if it's wrapping itself
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/711fb60a Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/711fb60a Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/711fb60a Branch: refs/heads/master Commit: 711fb60a10ca3f5cf77c7220f016bc1d2bbe9572 Parents: 5031d4f Author: Sam Corbett <[email protected]> Authored: Thu May 14 19:50:52 2015 +0100 Committer: Sam Corbett <[email protected]> Committed: Fri May 29 14:38:52 2015 +0100 ---------------------------------------------------------------------- .../exceptions/PropagatedRuntimeException.java | 25 ++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/711fb60a/utils/common/src/main/java/brooklyn/util/exceptions/PropagatedRuntimeException.java ---------------------------------------------------------------------- diff --git a/utils/common/src/main/java/brooklyn/util/exceptions/PropagatedRuntimeException.java b/utils/common/src/main/java/brooklyn/util/exceptions/PropagatedRuntimeException.java index 8327d21..07f4ca7 100644 --- a/utils/common/src/main/java/brooklyn/util/exceptions/PropagatedRuntimeException.java +++ b/utils/common/src/main/java/brooklyn/util/exceptions/PropagatedRuntimeException.java @@ -18,36 +18,53 @@ */ package brooklyn.util.exceptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** Indicates a runtime exception which has been propagated via {@link Exceptions#propagate} */ public class PropagatedRuntimeException extends RuntimeException { private static final long serialVersionUID = 3959054308510077172L; + private static final Logger LOG = LoggerFactory.getLogger(PropagatedRuntimeException.class); - final boolean causeEmbeddedInMessage; + private final boolean causeEmbeddedInMessage; /** Callers should typically *not* attempt to summarise the cause in the message here; use toString() to get extended information */ public PropagatedRuntimeException(String message, Throwable cause) { super(message, cause); + warnIfWrapping(cause); causeEmbeddedInMessage = message.endsWith(Exceptions.collapseText(getCause())); } public PropagatedRuntimeException(String message, Throwable cause, boolean causeEmbeddedInMessage) { super(message, cause); + warnIfWrapping(cause); this.causeEmbeddedInMessage = causeEmbeddedInMessage; } public PropagatedRuntimeException(Throwable cause) { super("" /* do not use default message as that destroys the toString */, cause); + warnIfWrapping(cause); causeEmbeddedInMessage = false; } + private void warnIfWrapping(Throwable cause) { + if (LOG.isTraceEnabled() && cause instanceof PropagatedRuntimeException) { + LOG.trace("Wrapping a PropagatedRuntimeException in another PropagatedRuntimeException. Call chain:", new Exception()); + } + } + @Override public String toString() { - if (causeEmbeddedInMessage) return super.toString(); - else return Exceptions.appendSeparator(super.toString(), Exceptions.collapseText(getCause())); + if (causeEmbeddedInMessage) { + return super.toString(); + } else { + return Exceptions.appendSeparator(super.toString(), Exceptions.collapseText(getCause())); + } } - + public boolean isCauseEmbeddedInMessage() { return causeEmbeddedInMessage; } + }
