[jboss-jira] [JBoss JIRA] (LOGTOOL-124) Ability to provide exception producer
James Perkins (JIRA)
issues at jboss.org
Tue May 23 19:52:00 EDT 2017
[ https://issues.jboss.org/browse/LOGTOOL-124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13410913#comment-13410913 ]
James Perkins commented on LOGTOOL-124:
---------------------------------------
Just to confirm. For a {{Function}} the signature must resolve to {{java.util.function.Function<String, Throwable>}} where {{String}} is the message.
For a {{BiFunction}} the signature must resolve to {{java.util.function.BiFunction<String, Throwable, Throwable}} where the {{String}} is the message and the first {{Throwable}} is the cause.
Does that sound correct?
Example of the current output for a function:
{code:java|title=Interface}
@Message("The operation failed due to %s")
<T extends Throwable> T operationFailed(@Producer Function<String, T> function, String op);
{code}
{code:java|title=Generated}
private static final String operationFailed = "The operation failed due to %s";
protected String operationFailed$str() {
return operationFailed;
}
@Override
public final <T extends Throwable> T operationFailed(final java.util.function.Function<String, T> function, final String op) {
final T result = function.apply(String.format(getLoggingLocale(), operationFailed$str(), op));
final StackTraceElement[] st = result.getStackTrace();
result.setStackTrace(Arrays.copyOfRange(st, 1, st.length));
return result;
}
{code}
> Ability to provide exception producer
> -------------------------------------
>
> Key: LOGTOOL-124
> URL: https://issues.jboss.org/browse/LOGTOOL-124
> Project: Log Tool
> Issue Type: Feature Request
> Reporter: David Lloyd
> Assignee: James Perkins
> Priority: Minor
> Fix For: 2.1.0.Alpha3
>
>
> Sometimes you must use the same exception message for many different types of exception. It would be nice if you could give a parameter which can produce an exception, like this:
> {code}
> public interface MyLogs {
> // ...
> @Message(id = 1234, "The operation failed due to %s")
> <T extends Throwable> T operationFailed(@Producer Function<String, T> fn, String thing);
> }
> {code}
> And later at usage:
> {code}
> // ...
> throw MyLogs.log.operationFailed(IOException::new, "thingy");
> {code}
> A valid function parameter must return a type which is equal or assignable to the return type, and is assignable to Throwable. Other than that, it can be treated the same as as if you were dealing with a constructor class that has exactly one constructor, using the same matching/inference logic, meaning you could also accept BiFunction which accepts two arguments.
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
More information about the jboss-jira
mailing list