I am using Python 3 and the YouTube Data API V3 to fetch comments from a YouTube video. This particular video has around 280,000 comments. I am trying to write a while loop that will get as many comments as possible before hitting the quota limit and then breaking if the quota limit is reached.

My loop appears to be successfully calling next page tokens and appending the requested metadata to my list, but when the quota is reached, it doesn't end the loop, instead registering an HttpError, and not saving any of the correctly fetched comment data.

Here is my current code:

```py
# Get resources:

def get(resource, **kwargs):
    print(f'Getting {resource} with params {kwargs}')
    kwargs['key'] = API_KEY
    response = requests.get(url=f'{YOUTUBE_BASE_URL}/{resource}',
                            params=remove_empty_kwargs(kwargs))
    print(f'Response: {response.status_code}')
    return response.json()

# Getting ALL comments for a video:

def getComments(video_id):
    comments = []
    res = get('commentThreads', part='id,snippet,replies', maxResults=100, videoId=video_id)

    try:
        nextPageToken = res['nextPageToken']
    except TypeError:
        nextPageToken = None

    while (nextPageToken):
        try:
            res = get('commentThreads', part='id,snippet,replies', maxResults=100, videoId=video_id)

            for i in res['items']:
                comments.append(i)

            nextPageToken = res['nextPageToken']

        except HttpError as error:
            print('An error occurred: %s' % error)
            break

    return comments

test = 'video-id-here'
testComments = getComments(test)

```

So, what happens is this correctly seems to be looping through all the comments. But after some time, i.e., after it has looped through several hundred times, I get the following error:

```py

Getting commentThreads with params {'part': 'id,snippet,replies', 'maxResults': 100, 'videoId': 'real video ID shows here'}
Response: 403

KeyError                                  Traceback (most recent call last)
<ipython-input-39-6582a0d8f122> in <module>
----> 1 testComments = getComments(test)

<ipython-input-29-68952caa30dd> in getComments(video_id)
     12             res = get('commentThreads', part='id,snippet,replies', maxResults=100, videoId=video_id)
     13 
---> 14             for i in res['items']:
     15                 comments.append(i)
     16 

KeyError: 'items'

```

So, first I get the expected 403 respsonse from the API after some time, which indicates reaching the quota limit. Then it throws the error for 'items', but the reason this error is thrown is because it didn't catch anymore comment threads, so there are no more 'items' to append.

My expected result is that the loop will just break when the quota limit is reached and save the comment data it managed to fetch before reaching the quota.

I think this is probably related to my 'try' and 'except' handling, but I can't seem to figure out.

Thanks!