You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让GCP Workflow遇到404错误时保持循环运行?

GCP Workflow 循环遇404错误停止的解决方法

我在开发GCP Workflow时遇到一个问题——触发404错误时循环会停止。查阅GCP文档后仍未找到解决办法,想请教如何让循环在遇到404错误时继续运行。

返回404错误的是SendNotification部分。

原始代码

main:
  steps:
    - getOnboardingPersons:
        call: http.get
        args:
          url: ${sys.get_env("SERVER_URL") + "/GetPersonIds"}
          auth: 
            type: OIDC
        result: personsForOnboardingEvents
    - loopThroughResults:      
        try:
          for:
            value: onboardingPayload
            index: idx
            in: ${personsForOnboardingEvents.body}
            steps:
              - sendNotification:
                  call: SendNotification
                  args:
                    personIds: ${onboardingPayload.personIds}
                    notificationEvent: ${onboardingPayload.notificationEvent}
        except:
          as: e
          steps:
            - TypeError:
                return: "No person ids found in personsForOnboardingEvents."
            - known_errors:
                switch:
                  - condition: ${e.code == 404}
                    return: ${e.body}
            - unhandled_exception:
                return: ${e.message}
    - theEnd:
        return: ${personsForOnboardingEvents}

SendNotification:
    params: [personIds, notificationEvent]
    steps:
      - loop:
          try:
            for:
              value: personId
              index: idx
              in: ${personIds}
              steps:
                - httpCall:
                      call: http.post
                      args:
                        url: ${sys.get_env("SERVER_URL") + "/SendByPersonId"}
                        auth: 
                          type: OIDC
                        body:
                          PersonId: ${personId}
                          NotificationEventName: ${notificationEvent}
                      result: httpResult
          except:
            as: e
            steps:
              - unhandled_exception:
                  return: ${e.body}

personsForOnboardingEvents 数据示例

[
  {
    "personIds": [],
    "notificationEvent": "EventA"
  },
  {
    "personIds": [],
    "notificationEvent": "EventB"
  },
  {
    "personIds": [],
    "notificationEvent": "EventC"
  },
]

更新

我已调整代码,但仍存在问题:遍历ID时,只要有一个ID返回404,循环就会停止:

SendNotification:
    params: [personIds, notificationEvent]
    steps:
      - loop:
          for:
            value: personId
            index: idx
            in: ${personIds}
            steps:
              - httpCall:
                  try:
                    call: http.post
                    args:
                      url: ${sys.get_env("SERVER_URL") + "/Communication/SendNotificationByPerson"}
                      auth: 
                        type: OIDC
                      body:
                        PersonId: ${personId}
                        NotificationEventName: ${notificationEvent}
                    result: httpResult
                  except:
                    as: e
                    steps:
                      - known_errors:
                          switch:
                          - condition: ${e.code == 404}
                            return: ${e.body}

解决方案

问题核心是except块中的return语句——一旦触发404就直接返回,导致整个SendNotification子流程终止,外层循环也随之停止。要让循环继续执行,需移除return,替换为错误记录或空操作,确保错误处理完成后流程回到循环,继续处理下一个personId。

修改后的SendNotification流程示例:

SendNotification:
    params: [personIds, notificationEvent]
    steps:
      - loop:
          for:
            value: personId
            index: idx
            in: ${personIds}
            steps:
              - httpCall:
                  try:
                    call: http.post
                    args:
                      url: ${sys.get_env("SERVER_URL") + "/Communication/SendNotificationByPerson"}
                      auth: 
                        type: OIDC
                      body:
                        PersonId: ${personId}
                        NotificationEventName: ${notificationEvent}
                    result: httpResult
                  except:
                    as: e
                    steps:
                      - known_errors:
                          switch:
                          - condition: ${e.code == 404}
                            steps:
                              # 记录404错误日志,不终止流程
                              - log404Error:
                                  call: sys.log
                                  args:
                                    text: ${personId + " 不存在,返回404错误"}
                                    severity: "WARNING"
                      # 处理其他类型错误,同样记录日志而非返回
                      - logOtherErrors:
                          call: sys.log
                          args:
                            text: ${处理personId: " + personId + " 时出错,错误信息: " + e.message}
                            severity: "ERROR"

关键注意点

  • 避免在循环内部的except块使用return,否则会直接终止当前子流程,中断外层循环
  • 遇到404时,使用sys.log记录错误信息(或其他非终止操作),保证流程能回到循环继续处理下一个元素
  • 外层loopThroughResults的except块若存在return,需确认SendNotification内部已处理所有预期错误,避免主循环被意外终止

内容的提问来源于stack exchange,提问作者Matheus Tonon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 17:40:58