I'm using a third party API which emits an HttpError
.
By catching this error I can inspect the http response status and narrow down the problem. So now I would like to emit a more specific HttpError which I'll dub BackendError
and RatelimitError
. The latter has context variables to be added.
How do I create a custom exception which inherits from HttpError and can be created without losing the original exception?
The question is actually polymorphism 101 but my head is fuzzy today:
class BackendError(HttpError):
"""The Google API is having it's own issues"""
def __init__(self, ex):
# super doesn't seem right because I already have
# the exception. Surely I don't need to extract the
# relevant bits from ex and call __init__ again?!
# self = ex # doesn't feel right either
try:
stuff()
except HttpError as ex:
if ex.resp.status == 500:
raise BackendError(ex)
How do we catch the original HttpError and encapsulate it so it is still recognisable as both an HttpError and a BackendError?