在Google Cloud Endpoints中返回自定义HTTP错误原因

Doc*_*ent 6 python google-app-engine google-cloud-endpoints

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

{
 "error": {
  "errors": [
   {
"domain": "global",
"reason": "invalidBookshelfId",
"message": "Invalid bookshelf ID."
   }
  ],
  "code": 400,
  "message": "Invalid bookshelf ID."
 }
}
Run Code Online (Sandbox Code Playgroud)

在Google Cloud Endpoints for Python中,有可能是例如raise endpoints.BadRequestException('Error message'),但除了错误消息之外,我还想返回"代码"或"原因",如上例所示.有没有办法实现这个目标?

bos*_*ter 1

该代码对应于 关联的状态代码BadRequestException。其他异常也被记录下来,例如endpoints.UnauthorizedException对应于状态代码401。至于reasondomain,这些是由 Google 的 API 基础设施设置的。

您心中有什么想法是无法实现的message