I am new to temporal, and I want to handle if invalid signals passed to the workflow,
I had defined 2 signals
export const s1 = wf.defineSignal(‘s1signal’);
export const s2 = wf.defineSignal(‘s2signal’);
and in the workflow I had called setHandler for both s1 and s2
during the workflow execution s1, s3 are the signals, I am passing, I am want to raise error if invalid signals are passed to the workflow.
Because signal handlers are dynamically registered, if a handler isn’t registered at the time a signal is received by the SDK, it is buffered in memory.
If you want a different behavior, you can implement a default signal handler to act as a catch-all and react to the situation.
What sort of error do you want to raise?
If you raise an ApplicationFailure
, the workflow will fail. If you raise a generic error, the workflow task will fail and your workflow will be “stuck”.
The latter will let you fix the situation by installing a handler for the given workflow.
You may just want to log an error for visibility and avoid failing the task or the entire workflow.
I just want to log error if an undefined or invalid signal lands on my workflow, which was not defined in my workflow, so can you please guide me how can I achieve these ?
Use Namespace: workflow | Temporal TypeScript SDK API Reference and log the error:
import * as workflow from '@temporalio/workflow'
export async function myWorkflow() {
workflow.setHandler(mySignalHandler, () => {})
// ... register all of the other known handlers here
workflow.setDefaultSignalHandler((signalName, ...args) => {
workflow.log.error('unknown signal', { signalName })
})
}
Thank you @bergundy , I had handle the invalid or undefined signal using switch cases, I will integrate this to my to my code.
You don’t need switch cases, you can set a handler per signal.
Maybe it wasn’t clear but this comment register all of the other known handlers here
tells you to register handlers above the “default” signal handler not in the body of that handler.