0

如何控制protoRPC响应中的 HTTP 状态码?

假设我有以下服务:

class ApiService(remote.Service):

    @remote.method(ApiRequestContextCreate, ApiResponseContextCreate)
    def context_create(self, request):
        cid = helpers.create_context(request.name)
        return ApiResponseContextCreate(cid=cid)

在我看来,protoRPC API 缺乏语义:要么可以满足请求并返回 a 200,要么引发异常以产生404. 当然可以在 RPC 方法中制作错误响应,但这似乎很笨拙。

更新:我发现我也可以ApplicationError400.

4

1 回答 1

0

HTTP 状态代码不是 ProtoRPC 规范的一部分,因为 ProtoRPC 旨在支持 HTTP 以外的协议。因此,它不能具有特定于 HTTP 的返回码。相反,错误是通过引发 ApplicationError 来返回的,使用 error_name 字段来指定应用程序特定的错误条件。

于 2012-11-27T00:55:54.063 回答