diff options
author | Arjun Satarkar <me@arjunsatarkar.net> | 2023-08-04 16:10:52 +0000 |
---|---|---|
committer | Arjun Satarkar <me@arjunsatarkar.net> | 2023-08-04 16:10:52 +0000 |
commit | f8aa1606b8733f629762f54785660305d4eb636e (patch) | |
tree | 223e364574b828eb5a57c8e3879d62ccc76ea06f | |
parent | 3da9b211b2d0b552859ec24c05ba26d18d887f2b (diff) | |
download | tagrss-f8aa1606b8733f629762f54785660305d4eb636e.tar tagrss-f8aa1606b8733f629762f54785660305d4eb636e.tar.gz tagrss-f8aa1606b8733f629762f54785660305d4eb636e.zip |
Gracefully handle more user input edge cases
-rwxr-xr-x | serve.py | 23 |
1 files changed, 18 insertions, 5 deletions
@@ -40,6 +40,13 @@ storage_path: pathlib.Path = pathlib.Path(args.storage_path) core = tagrss.TagRss(storage_path=storage_path) +def forgiving_parse_int(inp, default: int) -> int: + try: + return int(inp) + except (ValueError, TypeError): + return default + + def parse_space_separated_tags(inp: str) -> list[str]: tags: set[str] = set() tag = "" @@ -89,9 +96,12 @@ def validate_tags(tags: list[str]) -> typing.Optional[bottle.HTTPError]: def index(): per_page: int = min( MAX_PER_PAGE_ENTRIES, - int(bottle.request.query.get("per_page", DEFAULT_PER_PAGE_ENTRIES)), # type: ignore + forgiving_parse_int( + bottle.request.query.get("per_page"), # type: ignore + DEFAULT_PER_PAGE_ENTRIES, + ), ) - page_num = int(bottle.request.query.get("page_num", 1)) # type: ignore + page_num = forgiving_parse_int(bottle.request.query.get("page_num"), 1) # type: ignore offset = (page_num - 1) * per_page included_feeds_str: typing.Optional[str] = bottle.request.query.get( # type: ignore "included_feeds", None @@ -152,9 +162,12 @@ def index(): def list_feeds(): per_page: int = min( MAX_PER_PAGE_ENTRIES, - int(bottle.request.query.get("per_page", DEFAULT_PER_PAGE_ENTRIES)), # type: ignore + forgiving_parse_int( + bottle.request.query.get("per_page"), # type: ignore + DEFAULT_PER_PAGE_ENTRIES, + ), ) - page_num = int(bottle.request.query.get("page_num", 1)) # type: ignore + page_num = forgiving_parse_int(bottle.request.query.get("page_num"), 1) # type: ignore offset = (page_num - 1) * per_page total_pages: int = max(1, math.ceil(core.get_feed_count() / per_page)) feeds = core.get_feeds(limit=per_page, offset=offset, get_tags=True) @@ -254,7 +267,7 @@ def manage_feed_effect(): title=bottle.request.forms["title"], # type: ignore tags=parse_space_separated_tags(serialised_tags), ) - tag_validation_error = validate_tags(feed.tags) # type: ignore + tag_validation_error = validate_tags(feed.tags) # type: ignore if tag_validation_error: raise tag_validation_error try: |