Reputation: 1736
I have:
call_vendor_code()
that calls external vendor code;MyDomainException(Exceprion)
.I know that function call_vendor_code()
could throw MyDomainException
(because that logic in function is my). But how do I know which vendor code exceptions could be thrown in call_vendor_code()
in subcalls?
Example:
def call_vendor_code():
if not vendor.package.module.make_more_love():
raise MyDomainException('Not enough love.')
Vendor code could throw any other exceptions that I don't handle. It is possible to intercept all excepions from vendor code, but i thing that it is bad solution.
def call_vendor_code():
try:
if not vendor.package.module.make_more_love():
raise MyDomainException('Not enough love.')
except Exception, e:
raise MyDomainException(e)
How to interact/not interact with other-level exceptions?
Upvotes: 2
Views: 1801
Reputation: 7450
Your logic is sound.
How you handle these vendor specific exceptions, more elegantly, depends on the type of exceptions raised by the vendor code.
Are they plain python exceptions(e.g. ValueError, TypeError, etc)? Then
you don't have much of a choice. You should wrap them under a generic
except Exception
statement. In these unfortunate cases, many people
try to differentiate the type of error by parsing the exception message.
A major anti-pattern, but what can you do?
Do they raise their own vendor specific exceptions(best case scenario)? Then you can easily differentiate and handle them more elegantly in your code. They should have some kind of documentation about it :) Or, if the code is available, read through the code and see for yourself.
If we're talking about vendor code that talks to some external system(some kind of DB for example), they might have a generic VendorException with some internal error code(1133, 623, whatever) that is stored in the exception instance as an attribute. Again, they most probably will have a listing of those error codes, along with their description in some kind of documentation, that you can use to map them into your own custom exceptions.
Upvotes: 2