Hi. I need to catch all workflow exceptions and convert them to real exception based by ‘type’ field, but I don’t want to write try-catch blocks ewerywhere. Is there a way to create a global exception handler in temporal? Some sort of cross-cutting logic to catch WorkflowFailedException and throw original exception (or handle).
Catch WorkflowFailedException and throw the original exception to which caller?
Yes, but i want to do it in one place for every workflows (or activities) calls. Something like aspect programming.
Can i inject my custom exception handler?
You can write an interceptor that does this. I’m not sure if such a feature wouldn’t create more problems than solve them. For example, many exceptions are not serializable at all.
I’ve already tried this (throw original exception in interceptor), but each thrown exceptions were wrapped in WorkflowFailedException. I think it happpens in WorkflowExecutionHandler.runWorkflowMethod() method, because in code iseen some lines like:
try {
… - here execution call
} catch(Exception e) {
applyWorkflowPolicyAndRethorow(e); - here throw wrapped exception
}
I think you’ll have to unwrap in the client side interceptor.
Unfortunally, it doesn’t work.
@Override
public <R> GetResultOutput<R> getResult(GetResultInput<R> input) {
try {
return super.getResult(input);
} catch (Exception e) {
System.out.println("ExceptionWorkflowClientsCallInterceptor");
throw new CustomRuntimeException("client"); - "original" exception here
}
}
Exception will wrap (by WorkflowExecutionHandler or WorkflowStubImpl i think)
In sout i see, that my WorkflowClientsCallInterceptor calling, but thrown exception is wrapped.
Then, I recommend stopping fighting the Temporal error handling and keeping the exceptions as they are. There is a reason for the wrapping as it keeps all the necessary information about the failure. For example, the wrapper exception contains information about the workflow/activity that failed.