I have a weird issue. For the sake of having a short minimum working example (MWE), let's assume that Connect() returns a urllib3.connection.HTTPConnection object. Let's also assume that I have some other exceptions bubbling up that I want to ignore if 'magicword' is found in the error message (not the actual word, but hey, this is a MWE).
MWE:
try:
conn_obj = Connect()
except Exception as e: # there are some other exceptions I want to ignoreif 'magicword' not in e.message:
print 'fatal error: {}'.format(e.message)
This works fine on my machine and prints 'fatal error' when encountered and ignores other exceptions (as it should in this case).
However, on a colleague's machine, the error isn't handled and instead crashes and creates a traceback. It's the exact same error on my machine, only his won't print and crashes instead of being handled. We both use the exact same OS (Windows 7).
Obviously not handling the specific exception isn't ideal, so I then tried that route:
from urllib3.exceptions import NewConnectionError
try:
conn_obj = Connect()
except NewConnectionError as nce:
print 'fatal error: {}'.format(e.message)
except Exception as e: # there are some other exceptions I want to ignoreif 'magicword' not in e.message:
print 'fatal error: {}'.format(e.message)
That didn't work either. It won't catch the exception on his box for some reason. Why might the exception be handled on my machine but not on his?
UPDATE:
The connection object is raised inside the pyelasticsearch third party library. I've always been able to catch it just fine, but it is not being caught using the same code on others' machines. Here is a file I wrote to test if the error was caught when explicitly raised:
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)
This test worked perfectly on both of our machines. So somehow by getting the third-party library involved something is inconsistent between our machines (this is actually in a pyinstaller-compiled binary, so library differences shouldn't come into play).
The urllib3 library raises different types of exceptions depending on the version being used, and the NewConnectionError exception you are trying to catch is only available in recent versions of the library. Therefore, if your colleague has an older version of the library installed, the exception will not be caught by your code and will instead create a traceback.
You can verify this by checking the version of the library you and your colleague have installed. You can do this by running pip show urllib3 in the command prompt.
To resolve this issue, you can explicitly specify the version of the library in your requirements file or you can use the specific exception of the library version that your colleague is using.
Another possible cause might be the difference in Python version and the difference in the way the exception is raised and handled in the different python version.
It would be good to check if you are using the same version of python and also check the way the exception is raised in the library that you are using in both systems.
Also, you can try running the script in the python environment and not in the pyinstaller-compiled binary to check if the issue is still there.