本文介绍错误处理的核心概念和应用示例,通过重试(Retry)和捕获(Catch)机制,帮助您有效应对工作流中的常见错误,提升流程稳定性。
基本概念
在云工作流中,集成操作可能遇到预期或非预期的错误。您可以选择通过重试机制继续尝试当前操作,或通过捕获策略引导流程至其他状态。错误处理主要包含两部分:
错误重试(Retry):自动重试失败的操作,尝试恢复执行。
错误捕获(Catch):捕获无法通过重试解决的错误,跳转至指定状态处理。
处理顺序:系统先执行Retry策略,若重试失败,再应用Catch策略。
支持错误处理的状态类型
以下状态类型支持在定义中配置retryRef
和catchRef
字段,以处理执行中的错误:
并行(Parallel)
迭代(Foreach)
操作(Operation)
错误处理属性
字段 | 类型 | 是否必选 | 描述 | 示例值 |
---|---|---|---|---|
retryRef | []Retry | 否 | 包含多个Retry 定义。参考下文Retry属性 。 | |
catchRef | []Catch | 否 | 包含多个Catch 定义。参考下文Catch属性 。 |
Retry属性
定义重试策略,尝试多次执行失败的操作。
字段 | 类型 | 是否必选 | 描述 | 示例值 |
---|---|---|---|---|
name | string | 是 | 策略名称,用于标识和描述重试规则。 | 404RetryPolicy |
onErrors | []string | 是 | 可匹配的错误类型列表,见常见错误类型。 | - MyCustomError - http.404 |
intervalSeconds | int32 | 是 | 重试间隔时间,单位秒。 | 1 |
maxAttempts | int32 | 是 | 最大尝试次数。 | 3 |
backoffRate | float | 否 | 下次重试间隔的倍率。 | 2.0 |
maxIntervalSeconds | int32 | 否 | 最大重试间隔时间,单位秒,最大86400。 | 5 |
说明:
若针对同一错误类型定义多条重试规则,系统优先应用匹配到的第一条规则。
Catch属性
定义捕获策略,处理重试后仍失败的错误,跳转至指定状态。
字段 | 类型 | 是否必选 | 描述 | 示例值 |
---|---|---|---|---|
onErrors | []string | 是 | 可匹配的错误类型列表,见常见错误类型。 |
|
transition | Transition | 否 | 参考流程定义介绍中Transition 字段说明 |
说明:
若针对同一错误类型定义多条捕获规则,系统优先应用匹配到的第一条规则。
常见错误类型
以下列出常见错误类型,其他集成服务的错误请参考对应文档。
错误类型 | 描述 |
---|---|
http.{ErrorCode} | HTTP请求返回非200的状态码,如:
|
cf.{ErrorCode} | 函数调用错误码,如cf.404、cf.500。 |
cf.InvalideArgument | 函数调用参数错误。 |
cf.ParseError | 函数调用成功但结果解析失败。 |
cf.Unknown | 函数调用成功但执行出错,未被捕获的错误。 |
ctapi.{ErrorCode} | 普通集成请求错误码,如ctapi.404、ctapi.500。 |
cloudflow.InvalideArgument | 云工作流执行参数错误。 |
cloudflow.Unkown | 云工作流执行未被捕获的错误。 |
使用示例
示例1:错误重试
若“FetchMyIp”节点产生http.404
或http.401
错误,系统按定义的重试策略尝试恢复,失败后跳转至Final
节点。
specVersion: '0.8'
version: '1.0'
name: test
start: Pass
states:
- name: Pass
type: Operation
actions:
- functionRef:
type: Noop
transition:
nextState: FetchMyIp
- name: FetchMyIp
type: Operation
actions:
- functionRef:
type: Http
arguments:
url: https://httpstat.us/random/401,404
headers:
User-Agent: "apifox"
retryRef:
- name: 404RetryPolicy
onErrors:
- http.404
intervalSeconds: 1
maxIntervalSeconds: 5
maxAttempts: 5
backoffRate: 2
- name: 401RetryPolicy
onErrors:
- http.401
intervalSeconds: 1
maxIntervalSeconds: 5
maxAttempts: 3
backoffRate: 1
transition:
nextState: Final
- name: Final
type: Operation
actions:
- functionRef:
type: Noop
end: true
说明:
http.404
:最多尝试5次,初始间隔1秒,倍率2.0,间隔为[1, 2, 4, 5, 5]秒(受maxIntervalSeconds限制)。http.401
:最多尝试3次,间隔固定1秒,倍率1.0。
示例2:错误捕获
若“FetchMyIp”节点调用产生http.404
或http.401
错误,系统捕获错误并跳转至Sleep
节点继续执行。
specVersion: '0.8'
version: '1.0'
name: test
start: Pass
states:
- name: Pass
type: Operation
actions:
- functionRef:
type: Noop
transition:
nextState: Sleep
- name: Sleep
type: Sleep
duration: 1s
transition:
nextState: FetchMyIp
- name: FetchMyIp
type: Operation
actions:
- functionRef:
type: Http
arguments:
url: https://httpstat.us/random/401,404,200
catchRef:
- onErrors:
- http.404
transition:
nextState: Sleep
- onErrors:
- http.401
transition:
nextState: Sleep
transition:
nextState: Final
- name: Final
type: Operation
actions:
- functionRef:
type: Noop
end: true
说明:
FetchMyIp
节点发起HTTP请求,URL随机返回401、404或200状态码。若返回
http.404
或http.401
,捕获后跳转至Sleep
节点,暂停1秒后重试。
示例3:复杂重试与捕获策略
结合重试和捕获,若“FetchMyIp”产生http.404
或http.401
错误,先重试,失败后捕获并跳转至Sleep
节点。
specVersion: '0.8'
version: '1.0'
name: test
start: Pass
states:
- name: Pass
type: Operation
actions:
- functionRef:
type: Noop
transition:
nextState: FetchMyIp
- name: FetchMyIp
type: Operation
actions:
- functionRef:
type: Http
arguments:
url: https://httpstat.us/random/401,404
headers:
User-Agent: "apifox"
retryRef:
- name: 404RetryPolicy
onErrors:
- http.404
intervalSeconds: 1
maxIntervalSeconds: 3
maxAttempts: 3
backoffRate: 2
- name: 401RetryPolicy
onErrors:
- http.401
intervalSeconds: 1
maxIntervalSeconds: 3
maxAttempts: 3
backoffRate: 1
catchRef:
- onErrors:
- http.404
transition:
nextState: Final
- onErrors:
- http.401
transition:
nextState: Final
transition:
nextState: Final
- name: Final
type: Operation
actions:
- functionRef:
type: Noop
end: true
说明:
http.404
:最多尝试3次,间隔[1, 2, 3]秒,均失败后捕获错误,跳转至Final
。http.401
:最多尝试3次,间隔1秒,均失败后捕获错误,跳转至Final
。