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#13193mekarpeles wants to merge 2 commits intomasterinternetarchive/openlibrary:masterfrom 13192/fix/subjects-none-work-countinternetarchive/openlibrary:13192/fix/subjects-none-work-countCopy head branch name to clipboard
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
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.
mekarpeles
commented
Jul 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Closes #13192. Opening a subject page (
/subjects/<name>) intermittently crashes withTypeError: %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) setsnum_found=None+error=<msg>as a paired sentinel whenever the Solr query itself errors.SubjectEngine.get_subject_async()(openlibrary/plugins/worksearch/subjects.py) propagatedresult.num_foundstraight intopage.work_countwith no guard, andsubjects.html's$ungettext(..., page.work_count, count=page.work_count)cannot formatNone.Fix
Subjectnow carrieserror(propagated fromresult.error).subjects.GET()checkssubj.errorexplicitly and renders a newsubjects/unavailable.htmltemplate with a 503 status — a distinct "temporarily unavailable" state — instead of falling through to the crash-pronesubjects.htmltemplate, or to the existing 404notfound.html(which would misrepresent a transient Solr error as "this subject has zero books").search/subjects.html,search/lists.html,search/authors.html) of checkingresponse.errorand surfacing it rather than silently treating an error as zero results.Testing
openlibrary/plugins/worksearch/tests/test_subjects.py:get_subject_asyncpropagatesresult.erroronto theSubjectwhen Solr errors (mockedrun_solr_query_async).subjects.GET()routes an errored subject to the unavailable template + 503, not the crash path or the notfound path.notfound.html, unaffected.openlibrary/plugins/worksearch/suite passes (74 tests).