Idiom for partial failures

Ethan Furman ethan at stoneleaf.us
Thu Feb 20 16:33:22 EST 2020


On 02/20/2020 09:30 AM, David Wihl wrote:

> I'm working on the Python client library for the Google Ads API. In some cases, we can start a request with a partial failure flag = True. This means that the request may contain say 1000 operations. If any of the operations fail, the request will return with a success status without an exception. Then the developer has to iterate through the list of operation return statuses to determine which specific ones failed.
> 
> I believe that it would be more idiomatic in Python (and other languages like Ruby) to throw an exception when one of these partial errors occur. That way there would be the same control flow if a major or minor error occurred.

What I have done in such circumstances is have a custom exception type, and store the errors there -- how much detail you save depends on how much you need to properly post-process the failed items.

For example:

# not tested

class PartialFailure(Exception):
     def __init__(self, failures):
         self.failures = failures

... lots of code ...
failures = []

try:
     an_operation_that_could_fail
except SomeException:
     failures.append(debugging info)

... more code ...

if failures:
     raise PartialFailure(failures)

# in the calling code
try:
     code_that_may_raise_partial_failures
except PartialFailure as e:
     for failure in e.failures:
        handle_failure

--
~Ethan~


More information about the Python-list mailing list