onFailure didn't trigger when throwing exception
haiweiwang73 opened this issue · 1 comments
haiweiwang73 commented
I am testing the onFailure method and not sure my use case will fit in this scenario. I am trying to test when exception thrown in one of the task, and in the consumer of the onFailure, it will re throw it into another exception. But it seems the consumer never gets triggered.
@Override
public Task<OrderDatagram> getTask() {
return null;
}
@Override
public Consumer1<OrderDatagram> consumer() {
Consumer1<OrderDatagram> myConsumer = (y) ->
{
Optional<Integer> version = Optional.of(1);
y.setVersion(version);
System.out.println(y);
throw new NullPointerException(); // Got error in null pointer
};
return myConsumer;
}
public Consumer1<Throwable> failConsumer() {
Consumer1<Throwable> myFailConsumer = (y) ->
{
System.out.println("The failure consumer has been called"+ y); // Never print out
throw new Exception(); // never thrown
};
return myFailConsumer;
}
public Task<OrderDatagram> handle(Task<OrderDatagram> prevTask){
Task<OrderDatagram> task = prevTask.andThen("next", consumer());
task.onFailure("print stack trace", failConsumer());
return task;
}
}
bolobobo commented
Let me make it clear. So you said that the consumer
is triggered and do throw the NPE, but the failConsumer
didn't throw the pure exception but still throw NPE? Or you mean both the consumer
and failConsumer
were never triggered?
- If it's the 1st case, that met expectation. Based on the parseq implementation, when you write
preTask.onFailure(exp ->{ throw new Exception(“Get failed”); })
, exceptions thrown by consumer(your lambda function) are ignored. “onFailure” is used in situations where consumer needs to be called after failure of this task. More details are here: https://static.javadoc.io/com.linkedin.parseq/parseq/3.0.6/com/linkedin/parseq/Task.html#onFailure-java.lang.String-com.linkedin.parseq.function.Consumer1-
If you want to rewrap your customized exception, you can useTask.recoverWith(...)
; - If it's the 2nd case, just make sure you have already submitted the root task to parseq engine.