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.

How do you compare `ErrorCode`s?

See original GitHub issue

If I have the following:

        results = ga_service.search(self.client_customer_id, query=query, page_size=page_size)

        try:
            yield from results

        except GoogleAdsException as ex:
            <'error handling here'>

There is a lot of information in ex. The examples in this repo print the exception request_id and the name of the status (ex.error.code().name)

Instead of accessing the status, which can be general, I want to access the actual failures, ex.failure.errors.

for error in ex.failure.errors:
    if error.code == <'not sure what to do here'>:
        <'process individual failure'>

A use-case I have is that I want to trigger a certain code path if we receive a CLIENT_CUSTOMER_ID_INVALID and a different one if we get a CUSTOMER_NOT_ACTIVE. Using the StatusCode, both of these errors have a status of UNAUTHENTICATED.

My thoughts are that you need to create your own ErrorCode, but I am not having success with that using

error_code = client.get_type('ErrorCode', version='v1')

Any ideas?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
BenRKarlcommented, May 2, 2019

@j-walker23 @wesleybowman Thanks for coordinating with each other, and sorry for my delayed response. Is it accurate to describe the issue you’re having generally as wanting to lookup the string name of an Enum field using the integer value pulled from a request object?

It looks like that’s the core problem - if not, or if I’m leaving off other problems, please add on here.

The issue of doing this type of look up is definitely a major pain point. It’s possible to do this a bit more easily by using the protobuf helpers, like this:

from google.protouf.json_format import MessageToDict

# e is GoogleAdsException
failure = e.failure
dict = MessageToDict(failure)

for error in dict['errors']:
    error_code = error['errorCode']

    if error_code['authenticationError'] == 'CUSTOMER_NOT_FOUND':
        raise AdwordsPermissionError('Customer Not Found', exception)

    if error_code['authorizationError'] == 'USER_PERMISSION_DENIED':
        raise AdwordsPermissionError('User Permission Denied', exception)

    if error_code['requestError'] == 'INVALID_CUSTOMER_ID':
        raise AdwordsInvalidClientCustomerId('Invalid Client Customer ID', exception)

Admittedly it’s not a whole lot better, but at least you don’t need to deal with the Enum protos directly.

One caveat about this solution is that it will generate errors if you are retrieving resource fields that contain a digit following an underscore, for example: video_quartile_25_rate. See this separate issue for more context: https://github.com/googleads/google-ads-python/issues/30

I’m hoping to get functionality added to this library that will more easily expose the Enum field names so external helpers like MessageToDict aren’t necessary. Hopefully that will come in the near future.

Let me know if you have any additional questions or thoughts here.

1reaction
BenRKarlcommented, May 10, 2019

@wesleybowman Thank you for the explanation! I’m working on determining if there’s a way that we can improve the interface for this. To be honest it won’t be an easy change, so it might take some time. I’m going to close this issue since it’s not directly related, but I’ll open a new one to track progress against making Enums easier to work with.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to compare Go errors - Stack Overflow
If you currently compare errors using == , use errors.Is instead. Example: if err == io.ErrUnexpectedEOF. becomes if errors.Is(err, io.
Read more >
Compare Data Tool Error Messages - Micro Focus
Error Message Description or Cause R... Compare list file is empty Compare list file has no meaningful content. Ad... Error in writing to file Error...
Read more >
Comparing Go error values - Bitfield Consulting
The Error method on the error interface exists for humans, not code. The contents of that string belong in a log file, or...
Read more >
Comparing error or error equality in Go (Golang)
Overview. First of all, what is meant by equality of the error? As you already know that error is represented by the error...
Read more >
Comparison of different error-correcting codes. - ResearchGate
Table 1 summarizes the difference between the DIRC and sev- eral existing error-correction codes.
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