Skip to content

Commit a1d988c

Browse files
committed
AIP-193: Move JSON representation details to the bottom, and clarify
This is more of an informative section than anything else - API designers only need to know about it in order to understand the JSON response they might see from curl etc.
1 parent 700ad05 commit a1d988c

1 file changed

Lines changed: 79 additions & 48 deletions

File tree

aip/general/0193.md

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,54 +36,6 @@ important to [set the right tone][writing-tone] when writing messages.
3636

3737
### Error Response
3838

39-
#### JSON representation
40-
41-
A JSON representation of an error response might look like the following
42-
example.
43-
44-
For the purposes of following best practices, it’s helpful to break the
45-
error response into sections. Note that the order in which you write the
46-
error message is often different from how the error is presented to
47-
users.
48-
49-
50-
```json
51-
{
52-
"error": {
53-
"code": 429,
54-
"message": "The zone 'us-east1-a' does not have enough resources available to fulfill the request. Try a different zone, or try again later.",
55-
"status": "RESOURCE_EXHAUSTED",
56-
"details": [
57-
{
58-
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
59-
"reason": "RESOURCE_AVAILABILITY",
60-
"domain": "compute.googleapis.com",
61-
"metadata": {
62-
"zone": "us-east1-a",
63-
"vmType": "e2-medium",
64-
"attachment": "local-ssd=3,nvidia-t4=2",
65-
"zonesWithCapacity": "us-central1-f,us-central1-c"
66-
}
67-
},
68-
{
69-
"@type": "type.googleapis.com/google.rpc.LocalizedMessage",
70-
"locale": "en-US",
71-
"message": "An <e2-medium> VM instance with <local-ssd=3,nvidia-t4=2> is currently unavailable in the <us-east1-a> zone. Consider trying your request in the <us-central1-f,us-central1-c> zone(s), which currently has/have capacity to accommodate your request. Alternatively, you can try your request again with a different VM hardware configuration or at a later time. For more information, see the troubleshooting documentation."
72-
},
73-
{
74-
"@type": "type.googleapis.com/google.rpc.Help",
75-
"links": [
76-
{
77-
"description": "Additional information on this error",
78-
"url": "https://cloud.google.com/compute/docs/resource-error"
79-
}
80-
]
81-
}
82-
]
83-
}
84-
}
85-
```
86-
8739
#### google.rpc.Status
8840

8941
Services must return a [`google.rpc.Status`][Status] message when an API
@@ -458,6 +410,85 @@ If the user does have proper permission, but the requested resource or
458410
parent does not exist, the service **must** error with `NOT_FOUND` (HTTP
459411
404).
460412
413+
## HTTP/1.1+JSON representation
414+
415+
When clients use HTTP/1.1 as per [AIP-127](./0127.md), the error information
416+
is returned in the body of the response, as a JSON object. For backward
417+
compatibility reasons, this does not map precisely to `google.rpc.Status`,
418+
but contains the same core information. The schema is defined in the following proto:
419+
420+
```proto
421+
message Error {
422+
message Status {
423+
// The HTTP status code that corresponds to `google.rpc.Status.code`.
424+
int32 code = 1;
425+
// This corresponds to `google.rpc.Status.message`.
426+
string message = 2;
427+
// This is the enum version for `google.rpc.Status.code`.
428+
google.rpc.Code status = 4;
429+
// This corresponds to `google.rpc.Status.details`.
430+
repeated google.protobuf.Any details = 5;
431+
}
432+
433+
Status error = 1;
434+
}
435+
```
436+
437+
The most important difference is that the `code` field in the JSON is an HTTP status code,
438+
*not* the direct value of `google.rpc.Status.code`. For example, a `google.rpc.Status`
439+
message with a `code` value of 5 would be mapped to an object including the following
440+
code-related fields (as well as the message, details etc):
441+
442+
```json
443+
{
444+
"error": {
445+
"code": 404, // The HTTP status code for "not found"
446+
"status": "NOT_FOUND" // The name in google.rpc.Code for value 5
447+
}
448+
}
449+
```
450+
451+
The following JSON shows a fully populated HTTP/1.1+JSON representation of an error response.
452+
453+
454+
```json
455+
{
456+
"error": {
457+
"code": 429,
458+
"message": "The zone 'us-east1-a' does not have enough resources available to fulfill the request. Try a different zone, or try again later.",
459+
"status": "RESOURCE_EXHAUSTED",
460+
"details": [
461+
{
462+
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
463+
"reason": "RESOURCE_AVAILABILITY",
464+
"domain": "compute.googleapis.com",
465+
"metadata": {
466+
"zone": "us-east1-a",
467+
"vmType": "e2-medium",
468+
"attachment": "local-ssd=3,nvidia-t4=2",
469+
"zonesWithCapacity": "us-central1-f,us-central1-c"
470+
}
471+
},
472+
{
473+
"@type": "type.googleapis.com/google.rpc.LocalizedMessage",
474+
"locale": "en-US",
475+
"message": "An <e2-medium> VM instance with <local-ssd=3,nvidia-t4=2> is currently unavailable in the <us-east1-a> zone. Consider trying your request in the <us-central1-f,us-central1-c> zone(s), which currently has/have capacity to accommodate your request. Alternatively, you can try your request again with a different VM hardware configuration or at a later time. For more information, see the troubleshooting documentation."
476+
},
477+
{
478+
"@type": "type.googleapis.com/google.rpc.Help",
479+
"links": [
480+
{
481+
"description": "Additional information on this error",
482+
"url": "https://cloud.google.com/compute/docs/resource-error"
483+
}
484+
]
485+
}
486+
]
487+
}
488+
}
489+
```
490+
491+
461492
## Rationale
462493

463494
### Requiring ErrorInfo

0 commit comments

Comments
 (0)