question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose HttpError.reason when available

See original GitHub issue

Is there a better way to get the error reason than parsing HttpError.content from json string to object?

This is what I’m currently doing:

try:
    req = compute.zones().list(project=project_id, maxResults=max_results)
    res = req.execute()
    return res['items']
except HttpError as e:
    reason = json.loads(e.content)['error']['errors'][0]['reason']
    if reason == 'accessNotConfigured':
        <do the thing>

Ideally I’d like to avoid the call to json.loads().

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
busunkim96commented, Aug 19, 2020

I spoke to some other folks today, and I don’t think there’s a error response structure we can count on across all the APIs Google publishes.

If you need a particular field in an error response, parsing it from the JSON is the best option.

1reaction
busunkim96commented, Aug 18, 2020

Hi,

Thanks for the additional context.

I’m not sure how consistent APIs are about defining errors. The YouTube Data API and Drive API seem to have a similar structure.

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalidParameter",
    "message": "Invalid string value: 'asdf'. Allowed values: [mostpopular]",
    "locationType": "parameter",
    "location": "chart"
   }
  ],
  "code": 400,
  "message": "Invalid string value: 'asdf'. Allowed values: [mostpopular]"
 }
}
{
  "error": {
    "code": 400,
    "errors": [
      {
        "domain": "global",
        "location": "orderBy",
        "locationType": "parameter",
        "message": "Sorting is not supported for queries with fullText terms. Results are always in descending relevance order.",
        "reason": "badRequest"
      }
    ],
    "message": "Sorting is not supported for queries with fullText terms. Results are always in descending relevance order."
  }
}

The Analytics API is different - the status seems to be the equivalent of reason.

{
 "error": {
  "code": 403,
  "message": "User does not have sufficient permissions for this profile.",
  "status": "PERMISSION_DENIED"
 }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I return HTTP error code without default template in ...
http.responses[status_code] returns the error code text like "page not found" based on the status code.
Read more >
What Does HTTP Error 503 (Service Unavailable) Mean ...
A 503 Service Unavailable Error is an HTTP response status code that indicates your web server operates properly, but it can't handle a ......
Read more >
How to pass custom reason to 'write_error' through 'raise ...
I want to use 'raise tornado.web.HTTPError(400, reason='invalid request')' to pass a custom reason to the error response, and I hope to do this...
Read more >
HTTP response status codes - MDN Web Docs - Mozilla
This code indicates that the server has received and is processing the request, but no response is available yet.
Read more >
HTTP Errors <httpErrors> - Microsoft Learn
You can use the <remove> element to remove a specific error message from the collection of error messages your site or application inherits...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found