aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArjun Satarkar <me@arjunsatarkar.net>2023-08-04 16:10:52 +0000
committerArjun Satarkar <me@arjunsatarkar.net>2023-08-04 16:10:52 +0000
commitf8aa1606b8733f629762f54785660305d4eb636e (patch)
tree223e364574b828eb5a57c8e3879d62ccc76ea06f
parent3da9b211b2d0b552859ec24c05ba26d18d887f2b (diff)
downloadtagrss-f8aa1606b8733f629762f54785660305d4eb636e.tar
tagrss-f8aa1606b8733f629762f54785660305d4eb636e.tar.gz
tagrss-f8aa1606b8733f629762f54785660305d4eb636e.zip
Gracefully handle more user input edge cases
-rwxr-xr-xserve.py23
1 files changed, 18 insertions, 5 deletions
diff --git a/serve.py b/serve.py
index 64110d1..9ad398f 100755
--- a/serve.py
+++ b/serve.py
@@ -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: