launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #29737
[Merge] ~cjwatson/lp-archive:invalid-datetime into lp-archive:main
Colin Watson has proposed merging ~cjwatson/lp-archive:invalid-datetime into lp-archive:main.
Commit message:
Handle invalid datetimes more gracefully
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~cjwatson/lp-archive/+git/lp-archive/+merge/438640
Timestamp strings which match the regular expression we use but which don't represent any valid timestamp (such as a nonexistent day in a given month) should result in HTTP 404 errors rather than 500.
--
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/lp-archive:invalid-datetime into lp-archive:main.
diff --git a/lp_archive/routing.py b/lp_archive/routing.py
index 9cf5caf..c82619d 100644
--- a/lp_archive/routing.py
+++ b/lp_archive/routing.py
@@ -5,7 +5,7 @@
from datetime import datetime, timezone
-from werkzeug.routing import BaseConverter
+from werkzeug.routing import BaseConverter, ValidationError
class PrimaryArchiveConverter(BaseConverter):
@@ -37,9 +37,12 @@ class TimestampConverter(BaseConverter):
regex = r"[0-9]{8}T[0-9]{6}Z"
def to_python(self, value: str) -> datetime:
- return datetime.strptime(value, "%Y%m%dT%H%M%SZ").replace(
- tzinfo=timezone.utc
- )
+ try:
+ return datetime.strptime(value, "%Y%m%dT%H%M%SZ").replace(
+ tzinfo=timezone.utc
+ )
+ except ValueError:
+ raise ValidationError()
def to_url(self, value: datetime) -> str:
return value.strftime("%Y%m%dT%H%M%SZ")
diff --git a/tests/test_routing.py b/tests/test_routing.py
index 11f9c9a..f9c3bb7 100644
--- a/tests/test_routing.py
+++ b/tests/test_routing.py
@@ -126,3 +126,10 @@ def test_timestamp_invalid(app, client):
).status_code
== 404
)
+ assert (
+ client.get(
+ "/+test/20230229T000000Z",
+ headers=[("Host", "snapshot.ubuntu.test")],
+ ).status_code
+ == 404
+ )