In cbpro coinbase pro api I get this Error every once in a while.
Exception has occurred: KeyError 'open'
So, in order to try to go around it, I first tried to put in in a try: Exception: but I would still get the error, thus I tried to build a while as seen below.
try:
get24h = None
while get24h == None:
get24h = public_client.get_product_24hr_stats(product_id=symbol)
try:
open = None
while open == None:
open = get24h['open'] # HERE THE ERROR GETS TRIGGERED: Exception has occurred: KeyError 'open'
except ValueError as e:
print(f"ERROR GETTING 'open': {e}")
except ValueError as e:
print(f"ERROR GETTING get24h: {e}")
The thing is that since 'open' is not == None and I don't know what open is equal to when the error gets triggered, then I don't manage to find a way to avoid this from happened.
Furthermore, the code stops whenever I get this error, which is annoying
I can see why it's happening: I print the get24h right before I try to get the 'open' and I'm surpassing the 'Public rate limit'
get24h: {'message': 'Public rate limit exceeded'} open: None
Is there a way to go around this limit?
Now it's getting worse, because every time I load it, after just a few symbols, I get the error, so the code breaks, and I don't get to the results. This was working on and off until now, but I would manage to get the results several times per day without an error ( every 3 runs, one would fail), but now I don't even manage to make one to fully load.
Related
I am using a module from someone else to handle listening to a websocket.
As part of my testing I found that a simulated internet outage (by disabling/enabling my internet adapter) causes twisted.deferred to throw an error that doesn't get trapped by my exception handling.
It appear that it just prints the error message, instead of passing it on?
The problem is that then I cannot get it to gracefully handle it (e.g. keep checking the internet connection and resume processes when it is back up).
Is there anyway that I can 'listen' for such errors and trap them myself, or do I need to climb the tree and recode the other persons work?
I also use the signal module to catch 'Keyboard interrupt signal received' for manually stopping execution (not shown for brevity).
conn.start()
while True:
try:
time.sleep(1)
except Exception as err:
# Stop streams and logging gracefully
Print('Some other Exception received: ' + err)
conn.close()
user_log.close()
sys.exit(0)
I am scraping Google search pages using Python/Selenium, and since last night I have been encountering a MaxRetyError: p[Errno 61] Connection refused error. I debugged my code and found that the error begins in this code block right here"
domain = pattern.search(website)
counter = 2
# keep running this until the url appears like normal
while domain is None:
counter += 1
# close chrome and try again
print('link not found, closing chrome and restarting ...\nwaiting {} seconds...'.format(counter))
chrome.quit()
time.sleep(counter)
# chrome = webdriver.Chrome()
time.sleep(10) ### tried inserting a timer.sleep to delay request
chrome.get('https://google.com') ### error is right here. This is the second instance of chrome.get in this script
target = chrome.find_element_by_name('q')
target.send_keys(college)
target.send_keys(Keys.RETURN)
# parse the webpage
soup = BeautifulSoup(chrome.page_source, 'html.parser')
website = soup.find('cite', attrs={'class': 'iUh30'}).text
print('tried to get URL, is this it? : {}\n'.format(website))
pattern = re.compile(r'\w+\.(edu|com)')
domain = pattern.search(website)
I keep getting the following error:
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='ADDRESS', port=PORT): Max retries exceeded with url: /session/92ca3da95353ca5972fb5c520b704be4/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x11100e4e0>: Failed to establish a new connection: [Errno 61] Connection refused',))
As you can see in the code block above, I entered a timer.sleep() but it doesn't appear to help at all. For context, this script is part of a function, which in another script is repeatedly called in a loop. But again, I make sure to add delays in between each call of the webdriver.get() method. As of now, my script fails at the first iteration of this loop.
I tried googling the issue but the closest thing I found was this. It appears to speak to the same exact error, and the top answer identifies the same method that is causing the issue, but I don't really understand what the Solution and Conclusion sections are saying. I get that the MaxRetryError is confusing for debugging, but what precisely is the solution?
It mentions a max_retries argument, and Tracebacks, but I don't know what they mean in this context. Is there any way that I can catch this error (in the context of selenium)? I have some threads on Stack Exchange with mention of catching an error, but only in the context of urllib3. In my case, I would need to catch the same error for the Selenium package.
Thanks for any advice
My code still runs into issues every once in a while (which could be solved by using proxies), but I think I found the source of the issue. This loop anticipates that the first pattern match will return a .edu or .com, but does not anticipate for a .org. Therefore, my code runs indefinitely when the first search result returns a .org. Here is the source of the issue:
website = soup.find('cite', attrs={'class': 'iUh30'}).text
print('tried to get URL, is this it? : {}\n'.format(website))
pattern = re.compile(r'\w+\.(edu|com)') # does not anticipate .org's
Now my code runs okay, though I do run into errors when the code runs for too long (in this case the source of the issue is much clearer).
You are quitting the Chrome driver too early. After you call chrome.quit() it will cause subsequent calls to chrome.get('https://google.com') to fail and then automated retries lead to the MaxRetryError.
Try removing the call to chrome.quit().
How to raise an exception from the response message e.g {'status': "insufficient credit"}
try:
new_order = api.request(r)
except as :
print(new_order['status'])
else:
print(new_order)
The response message has different key/values when successful/unsuccessful.
I would need to know what api.request is to answer your question exactly.
Without know what it is, I would suggest using traceback with the error, which is in the standard python library.
for example:
import traceback
try:
new_order = api.request(r)
except Exception as err:
print(err)
traceback.print_exc()
else:
print(new_order)
Also, if api.request is raising an exception, it wouldn't be related to any "status message" they send. If they are telling you about the error, then api.result will likely return an object/list with the status code and/or error message in it, and not raise an exception on your server (which I don't think is possible for them to do).
Think of it like this: your server side code executes api.request, which is probably either a GET or POST request to the api's server. If there was an error in your request, they will send you a message saying what the problem is (I assume). No where in this process will an exception be raised.
If the request is sent to their server, there shouldn't be any exceptions raised on your server.
an exception is raised when you call raise 'some exception here', or when there is a syntax error/other serious error. In either case, your code execution is immediately stopped, and your server would respond to your client with a 500 internal service error.
I have a python script that controls different test-instruments (signal generator, amplifier, spectrum analyzer...) to automate a test.
These devices communicate over ethernet or serial with the pc running this python script.
I wrote a class for each device that I use. The script starts with initializing an instance of those classes. Something like this:
multimeter = Multimeter(192.168.1.5,5025)
amplifier = Amplifier(192.168.1.9,5025)
stirrer = Stirrer('COM4',9600)
.....
This can co wrong in many ways (battery is low, device not turned on, cable not connected ... )
It's possible to catch the errors with try/catch - try-except:
try:
multimeter = Multimeter(192.168.1.5,5025)
amplifier = Amplifier(192.168.1.9,5025)
stirrer = Stirrer('COM4',9600)
.....
except:
multimeter.close()
amplifier.close()
stirrer.close()
But now the problem is inside the except code block... We are not sure if the initialization of the objects succeeded and if they exist. They may not exist and so we can't call the close() method.
Because creating the instances is just normal sequential code, I know that when creating an instance of one of my classes fails, all the instances of the other classes previous to that line of code succeed. So you can try to create an instance of every class and check if that fails or not, and if it fails closing the connections of all previous objects.
try:
multimeter = Multimeter(192.168.1.5,5025)
except:
#problem with the multimeter
print('error')
try:
amplifier = Amplifier(192.168.1.9,5025)
except:
#problem with the amplifier, but we can close the multimeter
multimeter.close()
try:
stirrer = Stirrer('COM4',9600)
except:
#problem with the stirrer, but we can close the multimeter and the
amplifier
multimeter.close()
amplifier.close()
....
But I think this is ugly code? In particular when the number of objects (here test instruments grows, this becomes unmanageable. And it's sensitive for errors when you want to add or remove an object... Is there a better way to be sure that all connections are closed? Sockets should be closed on failure so we can assign the ip-address and port to a socket the next time the script is executed. Same with the serial interfaces, if it's not closed, it will raise an error to because you can't connect to a serial interface that already is open...
Use a container to store already created instruments, and split your code in short, independent, manageable parts:
def create_instruments(defs):
instruments = {}
for key, cls, params in instruments_defs:
try:
instruments[key] = cls(*params)
except Exception as e:
print("failed to instanciate '{}': {}".format(key, e))
close_instruments(instruments)
raise
return instruments
def close_instruments(intruments):
for key, instrument in intruments.items():
try:
instrument.close()
except Exception as e:
# just mention it - we can't do much more anyway
print("got error {} when closing {}".format(e, key))
instruments_defs = [
#(key, classname, (param1, ...)
("multimeter", Multimeter, ("192.168.1.5", 5025)),
("amplifier", Amplifier, ("192.168.1.9" ,5025)),
("stirrer", Stirrer, ('COM4',9600)),
]
instruments = create_instruments(instruments_defs)
You may also want to have a look at context managers (making sure resources are properly released is the main reason of context managers) but it might not necessarily be the best choice here (depends on how you use those objects, how your code is structured etc).
In fact, the solution that I'm suggesting in my question is the easiest way to solve this issue. In the try block, the script tries to initialize the instances one by one.
If you close the objects in the same order that they're created in the try block, then closing the connection will succeed for every test instrument, except for the instruments that where not initialized because of the error that happened in the try block.
(see comments in code snippet)
try:
multimeter = Multimeter(192.168.1.5,5025) #succes
amplifier = Amplifier(192.168.1.9,5025) #succes
stirrer = Stirrer('COM4',9600) # error COM4 is not available --> jump to except
generator = Generator() #not initialized because of error in stirrer init
otherTestInstrument = OtherTestInsrument() #not initialized because of error in stirrer init
.....
except:
multimeter.close() #initialized in try, so close() works
amplifier.close() #initialized in try, so close() works
stirrer.close() #probably initialized in try, so close() works probably
generator.close() #not initialized, will raise error, but doesn't matter.
otherTestInstrument.close() #also not initialized. No need to close it too.
I have the following :
### running with Python 3
import logging
logger = logging.getLogger(__name__)
logger.setLevel('DEBUG')
logger.exception('this is an exception')
The output is:
this is an exception
NoneType: None
What do i need to do to get rid of that "NoneType: None" output?
stdlib logging is trying to append the exception info, but you don't have any exception info because you're not logging from within an except: block. The NoneType: None ultimately come from the result of a sys.exc_info() call, logging doesn't bother to check whether they're valid or not.
Lame workaround:
logger.exception('this is an exception', exc_info=False)
Slightly less lame:
logger.error('this is an exception...haha not really')
Best:
try:
# risky code
except Exception:
logger.exception('this is an exception')
If you have an exception instance that was caught earlier, and you want to log it, and you're not in an except block, this will do the right thing:
logger.exception('this is an exception', exc_info=myerror)