You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* don't recover from a panic in a defer
* make it its own section
* docs: merge go error handling page
* docs: add some additional go context
* docs: make more succinct
* fix up some existing issues
---------
Co-authored-by: Lenny Chen <lenny.chen@temporal.io>
Co-authored-by: Lenny Chen <55669665+lennessyy@users.noreply.github.com>
description: Configure Workflow timeouts, set Retry Policies, and manage Activity timeouts and Heartbeats using Temporal's Go SDK for optimized execution control.
16
+
description: Handle errors, configure Workflow timeouts, set Retry Policies, and manage Activity timeouts and Heartbeats using Temporal's Go SDK.
16
17
---
17
18
18
19
This page shows how to do the following:
19
20
21
+
-[Handle errors](#error-handling)
20
22
-[Set Workflow timeouts](#workflow-timeouts)
21
23
-[Set a Workflow Retry Policy](#workflow-retries)
22
24
-[Set Activity timeouts](#activity-timeouts)
23
25
-[Set a custom Activity Retry Policy](#activity-retries)
24
26
27
+
## Error handling {#error-handling}
28
+
29
+
Within a Workflow, an Activity or Child Workflow execution might fail. You can
30
+
handle errors differently based on the error type.
31
+
32
+
If the Activity returns an error as `errors.New()` or `fmt.Errorf()`, that error is converted into `*temporal.ApplicationError`.
33
+
34
+
If the Activity returns an error as `temporal.NewNonRetryableApplicationError("error message", details)`, that error is returned as `*temporal.ApplicationError`.
35
+
36
+
There are other types of errors such as `*temporal.TimeoutError`, `*temporal.CanceledError` and
37
+
`*temporal.PanicError`.
38
+
39
+
Here's an example of handling Activity errors within Workflow code that differentiates between different error types.
// handle Activity errors (errors created other than using NewApplicationError() API)
54
+
switch applicationErr.Type() {
55
+
case"CustomErrTypeA":
56
+
// handle CustomErrTypeA
57
+
case CustomErrTypeB:
58
+
// handle CustomErrTypeB
59
+
default:
60
+
// newer version of Activity could return new errors that Workflow was not aware of.
61
+
}
62
+
}
63
+
64
+
varcanceledErr *CanceledError
65
+
if errors.As(err, &canceledErr) {
66
+
// handle cancellation
67
+
}
68
+
69
+
vartimeoutErr *TimeoutError
70
+
if errors.As(err, &timeoutErr) {
71
+
// handle timeout, could check timeout type by timeoutErr.TimeoutType()
72
+
switch err.TimeoutType() {
73
+
case commonpb.ScheduleToStart:
74
+
// Handle ScheduleToStart timeout.
75
+
case commonpb.StartToClose:
76
+
// Handle StartToClose timeout.
77
+
case commonpb.Heartbeat:
78
+
// Handle heartbeat timeout.
79
+
default:
80
+
}
81
+
}
82
+
83
+
varpanicErr *PanicError
84
+
if errors.As(err, &panicErr) {
85
+
// handle panic, message and call stack are available by panicErr.Error() and panicErr.StackTrace()
86
+
}
87
+
}
88
+
```
89
+
90
+
### Panics and deferred functions
91
+
92
+
In Go, [`defer` schedules cleanup functions and `recover()` catches panics](https://go.dev/blog/defer-panic-and-recover) to prevent them from crashing the program.
93
+
This doesn't work the same way in Temporal Workflow code — you cannot `recover()` from a panic inside a `defer`.
94
+
Deferred functions that try to interact with the Temporal SDK during panic unwinding will re-panic immediately.
95
+
96
+
Use `defer` only for local cleanup.
97
+
Handle Temporal API cleanup through explicit error checks instead.
98
+
25
99
## Workflow timeouts {#workflow-timeouts}
26
100
27
101
**How to set Workflow timeouts using the Temporal Go SDK**
0 commit comments