[jboss-jira] [JBoss JIRA] (LOGTOOL-101) Exception transformation

James Perkins (JIRA) issues at jboss.org
Wed Jun 7 17:43:00 EDT 2017


    [ https://issues.jboss.org/browse/LOGTOOL-101?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13417963#comment-13417963 ] 

James Perkins commented on LOGTOOL-101:
---------------------------------------

If we add a parameter to the {{@Producer}} this might not be needed. Maybe something like {{@Producer(copyStackTrace=false}}, then the {{Function}} could just attempt to clone the exception.

{code:java}
@Message("Binding to %s failed")
<T extends IOException> T bindFailed(@Producer(copyStackTrace=false BiFunction<String, IOException, T> function, @Cause IOException cause, SocketAddress address);

throw ValidMessages.MESSAGES.bindFailed((msg, cause) -> {
    final IOException copy;
    if (cause instanceof BindException) {
        copy = new BindException(msg + ": " + cause.getMessage());
    } else {
        copy = new IOException(msg + ": " + cause.getMessage());
    }
    copy.setStackTrace(cause.getStackTrace());
    return copy;
}, new IOException("Failed IO exception"), address);
{code}

Note the {{copyStackTrace}} here refers to the reset of the stack trace removing the interface entry from the stack trace. A better name may be needed.

E.g the default implementation looks something like:
{code:java}
@Override
public final <T extends IOException> T bindFailed(final java.util.function.BiFunction<String, IOException, T> function, final IOException cause, final SocketAddress address) {
    final T result = function.apply(String.format(getLoggingLocale(), bindFailed$str(), address), cause);
    final StackTraceElement[] st = result.getStackTrace();
    result.setStackTrace(Arrays.copyOfRange(st, 1, st.length));
    return result;
}
{code}

So we'd just need property to indicate the resulting stack trace should not be set. Maybe {{setStackTrace=false}}?

> Exception transformation
> ------------------------
>
>                 Key: LOGTOOL-101
>                 URL: https://issues.jboss.org/browse/LOGTOOL-101
>             Project: Log Tool
>          Issue Type: Feature Request
>            Reporter: David Lloyd
>            Assignee: James Perkins
>             Fix For: 2.1.0.Alpha3
>
>
> I'd like to be able to transform an exception message.  What this means is, create a new exception, embed its message in my message, and copy its stack trace to mine.
> Something like this:
> {code}
> @Message(id = 1234, "Binding to %s failed")
> IOException bindFailed(SocketAddress bindAddress, @TransformException IOException cause)
> {code}
> This would yield a message like: "Binding to 1.2.3.4/1234 failed: Address already in use". Note the addition of the ": " and the source string.
> Note that I can almost do this now, except that there's no way to copy the exception stack:
> {code}
> @Message(id = 1234, "Binding to %s failed: %s")
> IOException bindFailed(SocketAddress bindAddress, IOException cause)
> {code}
> An alternative approach is to add an annotation for copying the stack trace:
> {code}
> @Message(id = 1234, "Binding to %s failed")
> IOException bindFailed(SocketAddress bindAddress, @CopyStackFrom IOException cause)
> {code}
> Note that in this case the original exception message is lost.  The next variant would preserve the message:
> {code}
> @Message(id = 1234, "Binding to %s failed: %s")
> IOException bindFailed(SocketAddress bindAddress, IOException cause, @CopyStackFrom IOException cause2)
> {code}
> And now to eliminate the duplication:
> {code}
> @Message(id = 1234, "Binding to %s failed: %s")
> IOException bindFailed(SocketAddress bindAddress, @CopyStackFrom @Pos(2) IOException cause)
> {code}
> Note that I don't recall if @Pos is 1- or 0-based, so I guessed.



--
This message was sent by Atlassian JIRA
(v7.2.3#72005)


More information about the jboss-jira mailing list