Skip to content

Commit f48b363

Browse files
authored
fix: catch ParsingError (#779)
* fix: move `path_strings` var inside `with` block * refactor: move lambda to local function * fix: catch `ParsingError` and return no results * refactor: move `ParsingError` handling out to `QtDriver` Reverts changes made to `test_search.py` and `enums.py`.
1 parent 634e1c7 commit f48b363

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

tagstudio/resources/translations/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
"status.library_version_found": "Found:",
206206
"status.library_version_mismatch": "Library Version Mismatch!",
207207
"status.results_found": "{count} Results Found ({time_span})",
208+
"status.results.invalid_syntax": "Invalid Search Syntax:",
208209
"status.results": "Results",
209210
"tag_manager.title": "Library Tags",
210211
"tag.add_to_search": "Add to Search",

tagstudio/src/core/library/alchemy/enums.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
from dataclasses import dataclass, replace
33
from pathlib import Path
44

5+
import structlog
56
from src.core.query_lang import AST as Query # noqa: N811
67
from src.core.query_lang import Constraint, ConstraintType, Parser
78

89
MAX_SQL_VARIABLES = 32766 # 32766 is the max sql bind parameter count as defined here: https://github.com/sqlite/sqlite/blob/master/src/sqliteLimit.h#L140
910

11+
logger = structlog.get_logger(__name__)
12+
1013

1114
class TagColorEnum(enum.IntEnum):
1215
DEFAULT = 1

tagstudio/src/core/library/alchemy/library.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,11 @@ def has_path_entry(self, path: Path) -> bool:
640640
return session.query(exists().where(Entry.path == path)).scalar()
641641

642642
def get_paths(self, glob: str | None = None) -> list[str]:
643+
path_strings: list[str] = []
643644
with Session(self.engine) as session:
644645
paths = session.scalars(select(Entry.path)).unique()
646+
path_strings = list(map(lambda x: x.as_posix(), paths))
645647

646-
path_strings: list[str] = list(map(lambda x: x.as_posix(), paths))
647648
return path_strings
648649

649650
def search_library(

tagstudio/src/qt/ts_qt.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
from src.core.library.alchemy.fields import _FieldID
6767
from src.core.library.alchemy.library import Entry, LibraryStatus
6868
from src.core.media_types import MediaCategories
69+
from src.core.query_lang.util import ParsingError
6970
from src.core.ts_core import TagStudioCore
7071
from src.core.utils.refresh_dir import RefreshDirTracker
7172
from src.core.utils.web import strip_web_protocol
@@ -659,24 +660,26 @@ def init_library_window(self):
659660
# in a global dict for methods to access for different DPIs.
660661
# adj_font_size = math.floor(12 * self.main_window.devicePixelRatio())
661662

663+
def _filter_items():
664+
try:
665+
self.filter_items(
666+
FilterState.from_search_query(self.main_window.searchField.text())
667+
.with_sorting_mode(self.sorting_mode)
668+
.with_sorting_direction(self.sorting_direction)
669+
)
670+
except ParsingError as e:
671+
self.main_window.statusbar.showMessage(
672+
f"{Translations["status.results.invalid_syntax"]} "
673+
f"\"{self.main_window.searchField.text()}\""
674+
)
675+
logger.error("[QtDriver] Could not filter items", error=e)
676+
662677
# Search Button
663678
search_button: QPushButton = self.main_window.searchButton
664-
search_button.clicked.connect(
665-
lambda: self.filter_items(
666-
FilterState.from_search_query(self.main_window.searchField.text())
667-
.with_sorting_mode(self.sorting_mode)
668-
.with_sorting_direction(self.sorting_direction)
669-
)
670-
)
679+
search_button.clicked.connect(_filter_items)
671680
# Search Field
672681
search_field: QLineEdit = self.main_window.searchField
673-
search_field.returnPressed.connect(
674-
lambda: self.filter_items(
675-
FilterState.from_search_query(self.main_window.searchField.text())
676-
.with_sorting_mode(self.sorting_mode)
677-
.with_sorting_direction(self.sorting_direction)
678-
)
679-
)
682+
search_field.returnPressed.connect(_filter_items)
680683
# Sorting Dropdowns
681684
sort_mode_dropdown: QComboBox = self.main_window.sorting_mode_combobox
682685
for sort_mode in SortingModeEnum:

0 commit comments

Comments
 (0)