Open beaconIOT opened 4 years ago
You need to capture the exception and load the text response to json:
`try:
@JustinHBird Thanks Justin- I'm going to give this a go right now!
@JustinHBird I just gave that a go but still having issues with the code freezing when I turn off wifi. First, I changed the syntax of your code to be:
try:
database.child(deviceID).child(path).set(value)
except Exception as e:
json_data = json.loads(e.args)
print(json_data['error']['message'])
And what I get now is code freezing right when the wifi get's turned off and then if I hit Ctrl+C it shows the following. Any advice would be amazing!
[code is running fine here then wifi turns off]
^X^Creceived SIGINT
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/requests/packages/urllib3/connectionpool.py", line 386, in _make_request
httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "myPythonCode.py", line 281, in <module>
schedule.run_pending()
File "/home/pi/.local/lib/python3.7/site-packages/schedule/__init__.py", line 563, in run_pending
default_scheduler.run_pending()
File "/home/pi/.local/lib/python3.7/site-packages/schedule/__init__.py", line 94, in run_pending
self._run_job(job)
File "/home/pi/.local/lib/python3.7/site-packages/schedule/__init__.py", line 147, in _run_job
ret = job.run()
File "/home/pi/.local/lib/python3.7/site-packages/schedule/__init__.py", line 466, in run
ret = self.job_func()
File "myPythonCode.py", line 269, in postTimestamp
database.child("lastConnected").child(deviceID).child("timestamp").set(int(time.time()))
File "/home/pi/.local/lib/python3.7/site-packages/pyrebase/pyrebase.py", line 300, in set
request_object = self.requests.put(request_ref, headers=headers, data=json.dumps(data, **json_kwargs).encode("utf-8"))
File "/home/pi/.local/lib/python3.7/site-packages/requests/sessions.py", line 533, in put
return self.request('PUT', url, data=data, **kwargs)
File "/home/pi/.local/lib/python3.7/site-packages/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/home/pi/.local/lib/python3.7/site-packages/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "/home/pi/.local/lib/python3.7/site-packages/requests/adapters.py", line 423, in send
timeout=timeout
File "/home/pi/.local/lib/python3.7/site-packages/requests/packages/urllib3/connectionpool.py", line 595, in urlopen
chunked=chunked)
File "/home/pi/.local/lib/python3.7/site-packages/requests/packages/urllib3/connectionpool.py", line 389, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.7/http/client.py", line 1336, in getresponse
response.begin()
File "/usr/lib/python3.7/http/client.py", line 306, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.7/http/client.py", line 267, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
File "/usr/lib/python3.7/ssl.py", line 1052, in recv_into
return self.read(nbytes, buffer)
File "/usr/lib/python3.7/ssl.py", line 911, in read
return self._sslobj.read(len, buffer)
KeyboardInterrupt`
It's hard to tell without looking at the code.
This Answer on SO might point you in the right direction
Sorry I wasn't able to help more, good luck!
Sorry if this is answered somewhere else, I've tried looking through all the issues/support documentation but can't find the answer or what I'm doing wrong. I'm just trying to handle connection/HTTP errors in my Pyrebase calls in my code but I can't get it to catch the exception. When I test (by turning wifi off on the rPi, it just hangs indefinitely. Below is the code I'm trying to catch the exception with: (and yes, I am also using
import requests
)try: database.child(deviceID).child(path).set(value) except requests.exceptions.HTTPError as e: response = e.args[0].response error = response.json()['error']