【问题标题】:Returning custom HTTP error reasons in Google Cloud Endpoints在 Google Cloud Endpoints 中返回自定义 HTTP 错误原因
【发布时间】:2013-03-29 15:28:28
【问题描述】:

Google 自己的 REST API 会在出现错误时返回详细的响应,例如:

{
 "error": {
  "errors": [
   {
"domain": "global",
"reason": "invalidBookshelfId",
"message": "Invalid bookshelf ID."
   }
  ],
  "code": 400,
  "message": "Invalid bookshelf ID."
 }
}

在适用于 Python 的 Google Cloud Endpoints 中,可以使用例如raise endpoints.BadRequestException('Error message'),但除了错误消息之外,我还想返回“代码”或“原因”,如上例所示。有什么方法可以实现吗?

【问题讨论】:

    标签: python google-app-engine google-cloud-endpoints


    【解决方案1】:

    该代码对应于与BadRequestException 关联的状态代码。其他例外是​​documented,例如endpoints.UnauthorizedException 对应于状态码401。至于reasondomain,则是由谷歌的API Infrastructure 设置的。

    你想到了什么不能在message 中设置?

    【讨论】:

    • 同一个 HTTP 状态码可能有不同的错误情况。如果 API 客户端想要对这些错误条件采取不同的行动,我们如何区分它们?当然,客户端可能会解析消息,但感觉就像是 hack。最好为消息和错误代码/原因设置单独的字段。
    • 问题是消息需要在客户端手动解析字符串,要么强制他创建异常然后处理,要么在 API 级别解析字符串。
    猜你喜欢
    • 1970-01-01
    • 2014-01-19
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 2013-12-13
    相关资源
    最近更新 更多