在 Python 云函数中引发异常并同时返回 HTTP 错误代码的最佳方法

Gui*_*ume 4 exception http python-3.x google-cloud-platform google-cloud-functions

我在合并向我传达的两个要求时遇到了一些麻烦。

我最初的要求是处理我创建的用于将 JSON 数据插入 BigQuery 的 GCP 云函数中的异常。基本逻辑如下:

import json
import google.auth
from google.cloud import bigquery

class SomeRandomException(Exception):
    """Text explaining the purpose of this exception"""

def main(request):
    """Triggered by another HTTP request"""

    # Instantiation of BQ client
    credentials, your_project_id = google.auth.default(
        scopes=["https://www.googleapis.com/auth/cloud-platform"]
    )
    client = bigquery.Client(credentials=credentials, project=your_project_id, )

    # JSON reading
    json_data: dict = request.get_json()

    final_message: str = ""
    table_name = "someTableName"

    for row in json_data:
        rows_to_insert: list = []                    
        rows_to_insert.append(row["data"])

        if rows_to_insert:
            errors: list = client.insert_rows_json(
                table_name, rows_to_insert, row_ids=[None] * len(rows_to_insert)
            )
            if errors == []:
                final_message = f"\n{len(rows_to_insert)} row(s) successfully inserted in table\n"
            else:
                raise SomeRandomException(
                    f"Encountered errors while inserting rows into table: {errors}"
                )

    return final_message
Run Code Online (Sandbox Code Playgroud)

(请注意,代码中还处理了其他异常,但我尝试简化上述逻辑以使事情更容易分析)

然后我收到第二个要求,说除了引发异常之外,我还必须返回 HTTP 错误代码。我在另一个 StackOverflow问题中发现很容易返回错误代码和一些消息。但我还没有找到任何可以清楚解释如何返回该错误代码并同时引发异常的内容。据我所知,raisereturn是互斥的语句。那么,有没有什么解决方案能够优雅地将异常处理和HTTP错误结合起来呢?

例如,下面的代码片段可以接受吗?或者有更好的方法吗?我真的很困惑,因为异常应该使用raise而 HTTP 代码需要return

else:
    return SomeRandomException(
        f"Encountered errors while inserting rows into table: {errors}"
    ), 500
Run Code Online (Sandbox Code Playgroud)

Jac*_*oon 8

以约翰的评论为基础。您希望在代码中引发异常,然后添加 try-except 来捕获/处理异常并返回错误。

class SomeRandomException(Exception):
    # add custom attributes if need be
    pass


try: 
    # your code ...
    else:
        raise SomeRandomException("Custom Error Message!")
except SomeRandomException as err:
    # caught exception, time to return error
    response = {
        "error": err.__class__.__name__,
        "message": "Random Exception occured!"
    }
    # if exception has custom error message
    if len(err.args) > 0:
        response["message"] = err.args[0]
    return response, 500
Run Code Online (Sandbox Code Playgroud)

要更进一步,您还可以将Cloud Logging与 Python 根记录器集成,以便在返回错误之前将错误记录到 Cloud Function 的日志中,如下所示: logging.error(err)

这只会有助于使日志在以后更容易查询。