user3914505
user3914505

Reputation: 41

Catching HTTPerror in python3 from stravalib function

My code (for the relevant part) is as follows

myactivities = []

from urllib.error import HTTPError

for activity in client.get_activities(after = activities_from, before=activities_to):
   print(activity)
   print(activity.id, activity.start_date)
   try:
       temp = client.get_activity_streams(activity.id, types=types)
   except urllib.error.HTTPError:
       print("HTTPError")
   myactivities.append(temp)

The error print I get is this:

HTTPError                                 Traceback (most recent call last)
<ipython-input-29-fc30cef1e4f3> in <module>()
      8     try:
----> 9         temp = client.get_activity_streams(activity.id, types=types)
     10     except urllib.error.HTTPError:

C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\client.py in get_activity_streams(self, activity_id, types, resolution, series_type)
   1174         # Pack streams into dictionary
-> 1175         return {i.type: i for i in streams}
   1176 

C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\client.py in <dictcomp>(.0)
   1174         # Pack streams into dictionary
-> 1175         return {i.type: i for i in streams}
   1176 

C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\client.py in __next__(self)
   1529     def __next__(self):
-> 1530         return self.next()
   1531 

C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\client.py in next(self)
   1535         if not self._buffer:
-> 1536             self._fill_buffer()
   1537         try:

C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\client.py in _fill_buffer(self)
   1506 
-> 1507         raw_results = self.result_fetcher(page=self._page, per_page=self.per_page)
   1508 

C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\protocol.py in get(self, url, check_for_errors, use_webhook_server, **kwargs)
    245         params = dict([(k, v) for k, v in kwargs.items() if not k in referenced])
--> 246         return self._request(url, params=params, check_for_errors=check_for_errors, use_webhook_server=use_webhook_server)
    247 

C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\protocol.py in _request(self, url, params, files, method, check_for_errors, use_webhook_server)
    169         if check_for_errors:
--> 170             self._handle_protocol_error(raw)
    171 

C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\protocol.py in _handle_protocol_error(self, response)
    214         if x is not None:
--> 215             raise x
    216 

HTTPError: 404 Client Error: Not Found [Record Not Found: []]

During handling of the above exception, another exception occurred:

NameError                                 Traceback (most recent call last)
<ipython-input-29-fc30cef1e4f3> in <module>()
      8     try:
      9         temp = client.get_activity_streams(activity.id, types=types)
---> 10     except urllib.error.HTTPError:
     11         print("HTTPError")
     12     myactivities.append(temp)

NameError: name 'urllib' is not defined

How can I catch that HTTPError succesfully? I tried all the suggestions in catch specific HTTP error in python but none helped.

Upvotes: 1

Views: 865

Answers (1)

Neel
Neel

Reputation: 21317

Try

myactivities = []

from urllib.error import HTTPError

for activity in client.get_activities(after = activities_from, before=activities_to):
   print(activity)
   print(activity.id, activity.start_date)
   try:
       temp = client.get_activity_streams(activity.id, types=types)
   except HTTPError:
       print("HTTPError")
   myactivities.append(temp)

You already imported HTTPError, you have to use directly.

Your code shows error.

NameError                                 Traceback (most recent call last)
<ipython-input-29-fc30cef1e4f3> in <module>()
      8     try:
      9         temp = client.get_activity_streams(activity.id, types=types)
---> 10     except urllib.error.HTTPError:
     11         print("HTTPError")
     12     myactivities.append(temp)

NameError: name 'urllib' is not defined

Which gives error because urllib is not accessible. You imported HTTPError from that.

Upvotes: 1

Related Questions