Skip to content

Commit 84fbf5d

Browse files
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)
1 parent 5df134e commit 84fbf5d

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

graphene/types/datetime.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ def parse_literal(cls, node):
3838

3939
@staticmethod
4040
def parse_value(value):
41-
return iso8601.parse_date(value).date()
42-
41+
try:
42+
return iso8601.parse_date(value).date()
43+
except iso8601.ParseError:
44+
return None
4345

4446
class DateTime(Scalar):
4547
'''
@@ -62,8 +64,10 @@ def parse_literal(cls, node):
6264

6365
@staticmethod
6466
def parse_value(value):
65-
return iso8601.parse_date(value)
66-
67+
try:
68+
return iso8601.parse_date(value)
69+
except iso8601.ParseError:
70+
return None
6771

6872
class Time(Scalar):
6973
'''
@@ -87,5 +91,8 @@ def parse_literal(cls, node):
8791

8892
@classmethod
8993
def parse_value(cls, value):
90-
dt = iso8601.parse_date('{}T{}'.format(cls.epoch_date, value))
91-
return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)
94+
try:
95+
dt = iso8601.parse_date('{}T{}'.format(cls.epoch_date, value))
96+
return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)
97+
except iso8601.ParseError:
98+
return None

0 commit comments

Comments
 (0)