From 84fbf5dc23351c26a852271aa6529a60bce13735 Mon Sep 17 00:00:00 2001 From: Anis Jonischkeit Date: Tue, 27 Feb 2018 10:00:20 +1000 Subject: [PATCH] Made DateTime types return GraphQLError on fail This change makes it so that when an incorrectly formatted date string gets passed to a Date / Time argument a GraphQLError is returned rather than a GraphQLLocatedError. Since Date / Time are types, their errors should not be in the same class as errors in your application. This is also inline with how other types work in graphene (graphene.Int, graphene.Float) --- graphene/types/datetime.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/graphene/types/datetime.py b/graphene/types/datetime.py index 3b0e3f13..b3e14682 100644 --- a/graphene/types/datetime.py +++ b/graphene/types/datetime.py @@ -38,8 +38,10 @@ class Date(Scalar): @staticmethod def parse_value(value): - return iso8601.parse_date(value).date() - + try: + return iso8601.parse_date(value).date() + except iso8601.ParseError: + return None class DateTime(Scalar): ''' @@ -62,8 +64,10 @@ class DateTime(Scalar): @staticmethod def parse_value(value): - return iso8601.parse_date(value) - + try: + return iso8601.parse_date(value) + except iso8601.ParseError: + return None class Time(Scalar): ''' @@ -87,5 +91,8 @@ class Time(Scalar): @classmethod def parse_value(cls, value): - dt = iso8601.parse_date('{}T{}'.format(cls.epoch_date, value)) - return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo) + try: + dt = iso8601.parse_date('{}T{}'.format(cls.epoch_date, value)) + return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo) + except iso8601.ParseError: + return None