This repository has been archived by the owner on Dec 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exception reporting to ISODatetime
Summary: We see exceptions from this validator in prod, but we only record the error message we return, which deprives us of the details of the real exception. This adds exception logging when we see failures so that we can see what is really happening. The added passing test case is a value that appears to fail in the wild. Test Plan: A new test. Reviewers: naphat Reviewed By: naphat Subscribers: changesbot, anupc Differential Revision: https://tails.corp.dropbox.com/D225616
- Loading branch information
Showing
3 changed files
with
25 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
from __future__ import absolute_import, print_function | ||
from __future__ import absolute_import | ||
|
||
from datetime import datetime | ||
|
||
import logging | ||
|
||
|
||
class ISODatetime(object): | ||
def __call__(self, value): | ||
# type: (str) -> datetime | ||
try: | ||
return datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%fZ') | ||
except Exception: | ||
logging.exception("Failed to parse datetime: %s", value) | ||
raise ValueError('Datetime was not parseable. Expected ISO 8601 with timezone: YYYY-MM-DDTHH:MM:SS.mmmmmmZ') |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from datetime import datetime | ||
|
||
import pytest | ||
from changes.api.validators.datetime import ISODatetime | ||
|
||
|
||
def test_isodatetime_valid(): | ||
validator = ISODatetime() | ||
assert validator('2016-09-01T19:46:18.9Z') == datetime(year=2016, | ||
month=9, | ||
day=1, | ||
hour=19, | ||
minute=46, | ||
second=18, | ||
microsecond=900000) | ||
|
||
|
||
def test_isodatetime_invalid(): | ||
with pytest.raises(ValueError): | ||
ISODatetime()('invalid') |