Workflow Timeouts - TypeScript SDK
This page shows how to do the following:
Raise and Handle Exceptions
In each Temporal SDK, error handling is implemented idiomatically, following the conventions of the language.
Temporal uses several different error classes internally — for example, CancelledFailure in the Typescript SDK, to handle a Workflow cancellation.
You should not raise or otherwise implement these manually, as they are tied to Temporal platform logic.
The one Temporal error class that you will typically raise deliberately is ApplicationFailure.
In fact, any other exceptions that are raised from your Typescript code in a Temporal Activity will be converted to an ApplicationError internally.
This way, an error's type, severity, and any additional details can be sent to the Temporal Service, indexed by the Web UI, and even serialized across language boundaries.
In other words, these two code samples do the same thing:
class InvalidChargeError extends Error {
constructor(message: string) {
super(message);
this.name = "InvalidChargeError";
Object.setPrototypeOf(this, CustomError.prototype);
}
}
if (chargeAmount < 0) {
throw new InvalidChargeError(`Invalid charge amount: ${chargeAmount} (must be above zero)`);
}
if (chargeAmount < 0) {
throw ApplicationFailure.create({
message: `Invalid charge amount: ${chargeAmount} (must be above zero)`,
type: 'InvalidChargeError',
});
}
Depending on your implementation, you may decide to use either method.
One reason to use the Temporal ApplicationFailure class is because it allows you to set an additional non_retryable parameter.
This way, you can decide whether an error should not be retried automatically by Temporal.
This can be useful for deliberately failing a Workflow due to bad input data, rather than waiting for a timeout to elapse:
if (chargeAmount < 0) {
throw ApplicationFailure.create({
message: `Invalid charge amount: ${chargeAmount} (must be above zero)`,
nonRetryable: true
});
}
You can alternately specify a list of errors that are non-retryable in your Activity Retry Policy.
Failing Workflows
One of the core design principles of Temporal is that an Activity Failure will never directly cause a Workflow Failure — a Workflow should never return as Failed unless deliberately.
The default retry policy associated with Temporal Activities is to retry them until reaching a certain timeout threshold.
Activities will not actually return a failure to your Workflow until this condition or another non-retryable condition is met.
At this point, you can decide how to handle an error returned by your Activity the way you would in any other program.
For example, you could implement a Saga Pattern that uses try and catch blocks to "unwind" some of the steps your Workflow has performed up to the point of Activity Failure.
You will only fail a Workflow by manually raising an ApplicationFailure from the Workflow code.
You could do this in response to an Activity Failure, if the failure of that Activity means that your Workflow should not continue:
try {
await addAddress();
} catch (err) {
if (err instanceof ActivityFailure && err.cause instanceof ApplicationFailure) {
log.error(err.cause.message);
throw err;
}
}
This works differently in a Workflow than raising exceptions from Activities.
In an Activity, any Typescript exceptions or custom exceptions are converted to a Temporal ApplicationFailure.
In a Workflow, any exceptions that are raised other than an explicit Temporal ApplicationFailure will only fail that particular Workflow Task and be retried.
This includes any typical Typescript runtime errors like an undefined error that are raised automatically.
These errors are treated as bugs that can be corrected with a fixed deployment, rather than a reason for a Temporal Workflow Execution to return unexpectedly.
Workflow Timeouts
How to set Workflow Timeouts using the Temporal TypeScript SDK
Each Workflow timeout controls the maximum duration of a different aspect of a Workflow Execution.
Before we continue, we want to note that we generally do not recommend setting Workflow Timeouts, because Workflows are designed to be long-running and resilient. Instead, setting a Timeout can limit its ability to handle unexpected delays or long-running processes. If you need to perform an action inside your Workflow after a specific period of time, we recommend using a Timer.
Workflow Timeouts are set when starting a Workflow using either the Client or Workflow API.
- Workflow Execution Timeout - restricts the maximum amount of time that a single Workflow Execution can be executed
- Workflow Run Timeout: restricts the maximum amount of time that a single Workflow Run can last
- Workflow Task Timeout: restricts the maximum amount of time that a Worker can execute a Workflow Task
The following properties can be set on the WorkflowOptions when starting a Workflow using either the Client or Workflow API:
await client.workflow.start(example, {
taskQueue,
workflowId,
// Set Workflow Timeout duration
workflowExecutionTimeout: '1 day',
// workflowRunTimeout: '1 minute',
// workflowTaskTimeout: '30 seconds',
});
Workflow retries
How to set Workflow retries using the Temporal TypeScript SDK
A Retry Policy can work in cooperation with the timeouts to provide fine controls to optimize the execution experience.
Use a Retry Policy to retry a Workflow Execution in the event of a failure.
Workflow Executions do not retry by default, and Retry Policies should be used with Workflow Executions only in certain situations.
The Retry Policy can be set through the WorkflowOptions.retry property when starting a Workflow using either the Client or Workflow API.
const handle = await client.workflow.start(example, {
taskQueue,
workflowId,
retry: {
maximumAttempts: 3,
maximumInterval: '30 seconds',
},
});