simonw/datasette
Pagination crashes when sorting by excluded column with _labels=on (KeyError not caught)
Summary
Context: The
_next_value_and_urlfunction indatasette/views/table.pyhandles pagination for table views, including computing the “next” page URL when sorting by columns.Bug: The function catches
IndexErrorwhen trying to access a sort column that’s missing from the SELECT, butCustomRowobjects raiseKeyErrorinstead.Actual vs. expected: When labels are expanded (
?_labels=on), rows becomeCustomRowobjects which raiseKeyErrorfor missing keys, but the code only catchesIndexError, causing theKeyErrorto propagate and crash the request.Impact: Any table view using
?_labels=onwith?_sort=COLUMNwhere the sort column is excluded via?_col=results in a 500 Internal Server Error, completely breaking pagination and even the initial page load.
Code with bug
Failing test
Test output:
Recommended fix
Change the exception handling to catch both IndexError and KeyError: