I have a strange problem. In order to have a short minimum working example (MWE), suppose Connect()an object is returned urllib3.connection.HTTPConnection. Suppose also that I have a few other exceptions that arise because I want to ignore if an error message is found 'magicword'(not the actual word, but hey, this is MWE).
MWE:
try:
conn_obj = Connect()
except Exception as e:
if 'magicword' not in e.message:
print 'fatal error: {}'.format(e.message)
This works fine on my machine and prints a "fatal error" on detection and ignores other exceptions (as it should be in this case).
However, the error is not processed on the colleague’s computer, but instead it crashes and tracing is created. This is exactly the same error on my machine, only it will not be printed and failed, but not processed. We use the same OS (Windows 7).
Obviously, handling a specific exception is not ideal, so I tried this route:
from urllib3.exceptions import NewConnectionError
try:
conn_obj = Connect()
except NewConnectionError as nce:
print 'fatal error: {}'.format(e.message)
except Exception as e:
if 'magicword' not in e.message:
print 'fatal error: {}'.format(e.message)
That didn't work either. For some reason, he will not catch the exception on his box. Why can an exception be handled on my machine, but not on it?
UPDATE:
The connection object is created inside the third-party pyelasticsearch library. I have always been able to catch it just fine, but it is not captured using the same code on other machines. Here is the file I wrote to check if an error was detected when explicitly raising:
from urllib3.exceptions import NewConnectionError
def error_test(test_num):
print '\n\n'
try:
if test_num == 1:
print 'TEST 1: See if NewConnectionError is caught specifically'
raise NewConnectionError('no pool', 'test one')
elif test_num == 2:
print 'TEST 2: See if RuntimeError is caught related to magicword'
raise RuntimeError('test two magicword catching test')
elif test_num == 3:
print 'TEST 3: See if RuntimeError is caught NOT related to magicword'
raise RuntimeError('test three')
except NewConnectionError as nce:
print 'Test 1 passed successfully.\n\n{}'.format(nce.message)
except Exception as e:
if 'magicword' not in e.message:
print 'Test 3 passed successfully.\n\n{}'.format(e.message)
else:
print 'Test 2 passed successfully.\n\n{}'.format(e.message)
error_test(1)
error_test(2)
error_test(3)
. , - , , - ( , pyinstaller, ).