Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
Add exception reporting to ISODatetime
Browse files Browse the repository at this point in the history
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
kylec1 committed Sep 2, 2016
1 parent 29f0028 commit 55ec291
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion changes/api/validators/datetime.py
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.
20 changes: 20 additions & 0 deletions tests/changes/api/validators/test_datetime.py
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')

0 comments on commit 55ec291

Please sign in to comment.