Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(subjects): don't crash on None work_count when Solr errors#13193

Draft
mekarpeles wants to merge 2 commits into
masterinternetarchive/openlibrary:masterfrom
13192/fix/subjects-none-work-countinternetarchive/openlibrary:13192/fix/subjects-none-work-countCopy head branch name to clipboard
Draft

fix(subjects): don't crash on None work_count when Solr errors#13193
mekarpeles wants to merge 2 commits into
masterinternetarchive/openlibrary:masterfrom
13192/fix/subjects-none-work-countinternetarchive/openlibrary:13192/fix/subjects-none-work-countCopy head branch name to clipboard

Conversation

@mekarpeles

Copy link
Copy Markdown
Member

Problem

Closes #13192. Opening a subject page (/subjects/<name>) intermittently crashes with TypeError: %d format: a real number is required, not NoneType, falling back to the default error template, when Solr errors or is slow to respond.

Root cause

SearchResponse.from_solr_result() (openlibrary/plugins/worksearch/code.py) sets num_found=None + error=<msg> as a paired sentinel whenever the Solr query itself errors. SubjectEngine.get_subject_async() (openlibrary/plugins/worksearch/subjects.py) propagated result.num_found straight into page.work_count with no guard, and subjects.html's $ungettext(..., page.work_count, count=page.work_count) cannot format None.

Fix

  • Subject now carries error (propagated from result.error).
  • subjects.GET() checks subj.error explicitly and renders a new subjects/unavailable.html template with a 503 status — a distinct "temporarily unavailable" state — instead of falling through to the crash-prone subjects.html template, or to the existing 404 notfound.html (which would misrepresent a transient Solr error as "this subject has zero books").
  • This matches the existing convention elsewhere in the codebase (search/subjects.html, search/lists.html, search/authors.html) of checking response.error and surfacing it rather than silently treating an error as zero results.

Testing

  • New tests in openlibrary/plugins/worksearch/tests/test_subjects.py:
    • get_subject_async propagates result.error onto the Subject when Solr errors (mocked run_solr_query_async).
    • subjects.GET() routes an errored subject to the unavailable template + 503, not the crash path or the notfound path.
    • Regression: a genuine zero-result subject (no error) still 404s via notfound.html, unaffected.
  • Full openlibrary/plugins/worksearch/ suite passes (74 tests).
  • Manual Docker verification pending — will update this description once complete.

SearchResponse.from_solr_result() sets num_found=None + error=<msg> as
a paired sentinel when the underlying Solr query fails, but subjects.py
propagated num_found straight into page.work_count with no guard,
crashing subjects.html's %(count)d formatting (TypeError). Now the
subject page checks result.error explicitly and renders a distinct
"temporarily unavailable" (503) state, matching the existing pattern
in search/subjects.html, search/lists.html, and search/authors.html.

Closes #13192.

@accesslint accesslint Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 1 issue across 1 rule.

Comment thread openlibrary/templates/subjects/unavailable.html Outdated
Comment thread openlibrary/templates/subjects/unavailable.html Outdated

@accesslint accesslint Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 All fixed. Great work.

@mekarpeles mekarpeles assigned cdrini and unassigned openlibrary-bot Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Subject page TypeError: %d format: a real number is required, not NoneType

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.