type lookuperror

Peter Otten __peter__ at web.de
Thu Aug 18 03:14:47 EDT 2016


meInvent bbird wrote:

> when try keystone_client.tenants.get
> got error,
> 
> isn't this method for all kinds of function?
> 
>>>> m = "4c9a0da00b904422a23341e35be7f8d7"
>>>> ten = checkexception(keystone_client.tenants.get,
>>>> tenant_id=checkexception(m.encode,encoding='ascii',errors='ignore'))
> Unexpected error: <class 'keystoneclient.apiclient.exceptions.NotFound'>
> None

That's because your and Chris' version of checkexception() have no explicit 
return statement and thus always return None. There doesn't seeem to be a 
tenant_id=None...

> On Thursday, August 18, 2016 at 10:22:43 AM UTC+8, Chris Angelico wrote:
>> On Thu, Aug 18, 2016 at 12:13 PM, meInvent bbird <jobmattcon at gmail.com>
>> wrote:
>> > would like to check errors for every function i run,
>> > got error type lookuperror
>> >
>> > def checkexception(func, **kwargs):
>> >     try:
>> >         result = func(*tuple(value for _, value in kwargs.iteritems()))
>> >     except:
>> >         print "Unexpected error:", sys.exc_info()[0]
>> >         try:
>> >             print(func.__doc__)
>> >         except:
>> >             print("no doc error")
>> >
>> 
>> I'm going to be brutally honest, and simply say that this is terrible
>> code. I'm not even going to _try_ to fix it. Instead, here's a
>> completely rewritten form:
>> 
>> def checkexception(func, *args, **kwargs):
>>     try:
>>         result = func(*args, **kwargs)
>>     except BaseException as e:
>>         print("Exception raised: %s" % e)
>>         try: print(func.__doc__)
>>         except AttributeError: pass
>>         raise # Let the exception keep happening.
>> 
>> But really, there are even better ways to do this. Just let the
>> exception happen, and then use something like ipython to help you
>> analyze the traceback.
>> 
>> ChrisA





More information about the Python-list mailing list