1
1
from math import isfinite
2
2
from typing import Any
3
3
4
+ from ..error import GraphQLError
4
5
from ..pyutils import inspect , is_finite , is_integer , FrozenDict
5
6
from ..language .ast import (
6
7
BooleanValueNode ,
@@ -51,19 +52,19 @@ def serialize_int(value: Any) -> int:
51
52
if num != float_value :
52
53
raise ValueError
53
54
except (OverflowError , ValueError , TypeError ):
54
- raise TypeError (f"Int cannot represent non-integer value: { inspect (value )} " )
55
+ raise GraphQLError (f"Int cannot represent non-integer value: { inspect (value )} " )
55
56
if not MIN_INT <= num <= MAX_INT :
56
- raise TypeError (
57
+ raise GraphQLError (
57
58
f"Int cannot represent non 32-bit signed integer value: { inspect (value )} "
58
59
)
59
60
return num
60
61
61
62
62
63
def coerce_int (value : Any ) -> int :
63
64
if not is_integer (value ):
64
- raise TypeError (f"Int cannot represent non-integer value: { inspect (value )} " )
65
+ raise GraphQLError (f"Int cannot represent non-integer value: { inspect (value )} " )
65
66
if not MIN_INT <= value <= MAX_INT :
66
- raise TypeError (
67
+ raise GraphQLError (
67
68
f"Int cannot represent non 32-bit signed integer value: { inspect (value )} "
68
69
)
69
70
return int (value )
@@ -72,11 +73,14 @@ def coerce_int(value: Any) -> int:
72
73
def parse_int_literal (ast , _variables = None ):
73
74
"""Parse an integer value node in the AST."""
74
75
if not isinstance (ast , IntValueNode ):
75
- raise TypeError (f"Int cannot represent non-integer value: { print_ast (ast )} " )
76
+ raise GraphQLError (
77
+ f"Int cannot represent non-integer value: { print_ast (ast )} " , ast
78
+ )
76
79
num = int (ast .value )
77
80
if not MIN_INT <= num <= MAX_INT :
78
- raise TypeError (
79
- f"Int cannot represent non 32-bit signed integer value: { print_ast (ast )} "
81
+ raise GraphQLError (
82
+ f"Int cannot represent non 32-bit signed integer value: { print_ast (ast )} " ,
83
+ ast ,
80
84
)
81
85
return num
82
86
@@ -103,20 +107,26 @@ def serialize_float(value: Any) -> float:
103
107
if not isfinite (num ):
104
108
raise ValueError
105
109
except (ValueError , TypeError ):
106
- raise TypeError (f"Float cannot represent non numeric value: { inspect (value )} " )
110
+ raise GraphQLError (
111
+ f"Float cannot represent non numeric value: { inspect (value )} "
112
+ )
107
113
return num
108
114
109
115
110
116
def coerce_float (value : Any ) -> float :
111
117
if not is_finite (value ):
112
- raise TypeError (f"Float cannot represent non numeric value: { inspect (value )} " )
118
+ raise GraphQLError (
119
+ f"Float cannot represent non numeric value: { inspect (value )} "
120
+ )
113
121
return float (value )
114
122
115
123
116
124
def parse_float_literal (ast , _variables = None ):
117
125
"""Parse a float value node in the AST."""
118
126
if not isinstance (ast , (FloatValueNode , IntValueNode )):
119
- raise TypeError (f"Float cannot represent non numeric value: { print_ast (ast )} " )
127
+ raise GraphQLError (
128
+ f"Float cannot represent non numeric value: { print_ast (ast )} " , ast
129
+ )
120
130
return float (ast .value )
121
131
122
132
@@ -142,20 +152,24 @@ def serialize_string(value: Any) -> str:
142
152
# do not serialize builtin types as strings, but allow serialization of custom
143
153
# types via their `__str__` method
144
154
if type (value ).__module__ == "builtins" :
145
- raise TypeError (f"String cannot represent value: { inspect (value )} " )
155
+ raise GraphQLError (f"String cannot represent value: { inspect (value )} " )
146
156
return str (value )
147
157
148
158
149
159
def coerce_string (value : Any ) -> str :
150
160
if not isinstance (value , str ):
151
- raise TypeError (f"String cannot represent a non string value: { inspect (value )} " )
161
+ raise GraphQLError (
162
+ f"String cannot represent a non string value: { inspect (value )} "
163
+ )
152
164
return value
153
165
154
166
155
167
def parse_string_literal (ast , _variables = None ):
156
168
"""Parse a string value node in the AST."""
157
169
if not isinstance (ast , StringValueNode ):
158
- raise TypeError (f"String cannot represent a non string value: { print_ast (ast )} " )
170
+ raise GraphQLError (
171
+ f"String cannot represent a non string value: { print_ast (ast )} " , ast
172
+ )
159
173
return ast .value
160
174
161
175
@@ -176,12 +190,14 @@ def serialize_boolean(value: Any) -> bool:
176
190
return value
177
191
if is_finite (value ):
178
192
return bool (value )
179
- raise TypeError (f"Boolean cannot represent a non boolean value: { inspect (value )} " )
193
+ raise GraphQLError (
194
+ f"Boolean cannot represent a non boolean value: { inspect (value )} "
195
+ )
180
196
181
197
182
198
def coerce_boolean (value : Any ) -> bool :
183
199
if not isinstance (value , bool ):
184
- raise TypeError (
200
+ raise GraphQLError (
185
201
f"Boolean cannot represent a non boolean value: { inspect (value )} "
186
202
)
187
203
return value
@@ -190,8 +206,8 @@ def coerce_boolean(value: Any) -> bool:
190
206
def parse_boolean_literal (ast , _variables = None ):
191
207
"""Parse a boolean value node in the AST."""
192
208
if not isinstance (ast , BooleanValueNode ):
193
- raise TypeError (
194
- f"Boolean cannot represent a non boolean value: { print_ast (ast )} "
209
+ raise GraphQLError (
210
+ f"Boolean cannot represent a non boolean value: { print_ast (ast )} " , ast
195
211
)
196
212
return ast .value
197
213
@@ -213,13 +229,13 @@ def serialize_id(value: Any) -> str:
213
229
# do not serialize builtin types as IDs, but allow serialization of custom types
214
230
# via their `__str__` method
215
231
if type (value ).__module__ == "builtins" :
216
- raise TypeError (f"ID cannot represent value: { inspect (value )} " )
232
+ raise GraphQLError (f"ID cannot represent value: { inspect (value )} " )
217
233
return str (value )
218
234
219
235
220
236
def coerce_id (value : Any ) -> str :
221
237
if not isinstance (value , str ) and not is_integer (value ):
222
- raise TypeError (f"ID cannot represent value: { inspect (value )} " )
238
+ raise GraphQLError (f"ID cannot represent value: { inspect (value )} " )
223
239
if isinstance (value , float ):
224
240
value = int (value )
225
241
return str (value )
@@ -228,8 +244,9 @@ def coerce_id(value: Any) -> str:
228
244
def parse_id_literal (ast , _variables = None ):
229
245
"""Parse an ID value node in the AST."""
230
246
if not isinstance (ast , (StringValueNode , IntValueNode )):
231
- raise TypeError (
232
- f"ID cannot represent a non-string and non-integer value: { print_ast (ast )} "
247
+ raise GraphQLError (
248
+ f"ID cannot represent a non-string and non-integer value: { print_ast (ast )} " ,
249
+ ast ,
233
250
)
234
251
return ast .value
235
252
0 commit comments