'urllib3.exceptions.NewConnectionError error handled on one machine but not on another
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 ignore
if '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 ignore
if '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).
Solution 1:[1]
Maybe you could have tried from elasticsearch.exceptions import ConnectionError and replace your NewConnectionError with that. Docs from elasticsearch here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shadi |
