GCP 工作流:处理 200 以外的 http 函数响应

问题描述

在 GCP 工作流中调用 http 端点时,只有 HttpStatus 200 被认为是成功的。

如何处理其他成功状态代码? 201、202等

示例工作流程示例:

- readItem:
    try:
      call: http.get
      args:
        url: https://example.com/someapi
        auth:
          type: OIDC
      result: APIResponse
    except:
      as: e
      steps:
        - kNownErrors:
            switch:
              - condition: ${not("HttpError" in e.tags)}
                next: connectionProblem
              - condition: ${e.code == 404}
                next: urlNotFound
              - condition: ${e.code == 403}
                next: authProblem
        - UnhandledException:
            raise: ${e}
- urlFound:
    return: ${APIResponse.body}
- connectionProblem:
    return: "Connection problem; check URL"
- urlNotFound:
    return: "Sorry,URL wasn't found"
- authProblem:
    return: "Authentication error"

如果 api 端点 https://example.com/someapi 返回 200 状态代码以外的任何内容,则调用 connectionProblem。

如果是 GET 或 POST 请求,这也是一样的。

处理这个问题的最佳方法是什么?

解决方法

在 Google Workflows 的文档中没有提到如何处理其他 200 秒的状态,所以我认为如果不将它们视为错误,这是不可能的。

这意味着为了做到这一点,您将需要添加一个额外的步骤来处理此状态作为错误处理策略,例如 - condition: ${e.code == 201}

或者,您可以打开一个 feature request in Google's Issue Tracker,以便他们可以考虑实现此类状态代码的不同处理方式,或者至少可以在文档中涉及更多详细信息。