WordPress / openverse-api

The Openverse API allows programmatic access to search for CC-licensed and public domain digital media.
https://api.openverse.engineering/v1
MIT License
77 stars 51 forks source link

Results may be `None` during tallying #1131

Closed AetherUnbound closed 1 year ago

AetherUnbound commented 1 year ago

Sentry link

https://openverse.sentry.io/share/issue/eaeee780feef4a96944d4e5ef4e6d39b/

Description

It appears as though the value of results can be None during the tallying step:

TypeError: 'NoneType' object is not subscriptable
  File "django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "django/utils/decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "catalog/api/views/media_views.py", line 88, in list
    results, num_pages, num_results = search_controller.search(
  File "catalog/api/controllers/search_controller.py", line 429, in search
    results_to_tally = results[: 80 - (max_result_depth - page_size)]

This is right after a log line, no image urls to validate

Reproduction

I am able to reproduce this with the URL: https://api.openverse.engineering/v1/images/?license=by,by-nd,by-sa,cc0,pdm&license_type=commercial&filter_dead=true&mature=false&size=medium,large&page_size=100&page=1&q=kobinet

Note: You must use an authenticated app to request page_size=100; I tried pages 1-10 of the base page_size and I was unable to reproduce the error.

AetherUnbound commented 1 year ago

Potentially introduced in #1113

sarayourfriend commented 1 year ago

@AetherUnbound Ahhh, interesting edge case. The falsey check on results needs to wrap the entire block of page size checks, right?

AetherUnbound commented 1 year ago

That's right! I'm working on a fix now that I'm about to get up for PR 😄