@@ -9,6 +9,8 @@ import isObjectLike from '../jsutils/isObjectLike';
9
9
import { Kind } from '../language/kinds' ;
10
10
import { print } from '../language/printer' ;
11
11
12
+ import { GraphQLError } from '../error/GraphQLError' ;
13
+
12
14
import { GraphQLScalarType , isScalarType } from './definition' ;
13
15
14
16
// As per the GraphQL Spec, Integers are only treated as valid when a valid
@@ -30,12 +32,12 @@ function serializeInt(value: mixed): number {
30
32
}
31
33
32
34
if ( ! isInteger ( num ) ) {
33
- throw new TypeError (
35
+ throw new GraphQLError (
34
36
`Int cannot represent non-integer value: ${ inspect ( value ) } ` ,
35
37
) ;
36
38
}
37
39
if ( num > MAX_INT || num < MIN_INT ) {
38
- throw new TypeError (
40
+ throw new GraphQLError (
39
41
`Int cannot represent non 32-bit signed integer value: ${ inspect ( value ) } ` ,
40
42
) ;
41
43
}
@@ -44,12 +46,12 @@ function serializeInt(value: mixed): number {
44
46
45
47
function coerceInt ( value : mixed ) : number {
46
48
if ( ! isInteger ( value ) ) {
47
- throw new TypeError (
49
+ throw new GraphQLError (
48
50
`Int cannot represent non-integer value: ${ inspect ( value ) } ` ,
49
51
) ;
50
52
}
51
53
if ( value > MAX_INT || value < MIN_INT ) {
52
- throw new TypeError (
54
+ throw new GraphQLError (
53
55
`Int cannot represent non 32-bit signed integer value: ${ inspect ( value ) } ` ,
54
56
) ;
55
57
}
@@ -64,14 +66,16 @@ export const GraphQLInt = new GraphQLScalarType({
64
66
parseValue : coerceInt ,
65
67
parseLiteral ( ast ) {
66
68
if ( ast . kind !== Kind . INT ) {
67
- throw new TypeError (
69
+ throw new GraphQLError (
68
70
'Int cannot represent non-integer value: ' + print ( ast ) ,
71
+ ast ,
69
72
) ;
70
73
}
71
74
const num = parseInt ( ast . value , 10 ) ;
72
75
if ( num > MAX_INT || num < MIN_INT ) {
73
- throw new TypeError (
76
+ throw new GraphQLError (
74
77
'Int cannot represent non 32-bit signed integer value: ' + ast . value ,
78
+ ast ,
75
79
) ;
76
80
}
77
81
return num ;
@@ -88,7 +92,7 @@ function serializeFloat(value: mixed): number {
88
92
num = Number ( value ) ;
89
93
}
90
94
if ( ! isFinite ( num ) ) {
91
- throw new TypeError (
95
+ throw new GraphQLError (
92
96
`Float cannot represent non numeric value: ${ inspect ( value ) } ` ,
93
97
) ;
94
98
}
@@ -97,7 +101,7 @@ function serializeFloat(value: mixed): number {
97
101
98
102
function coerceFloat ( value : mixed ) : number {
99
103
if ( ! isFinite ( value ) ) {
100
- throw new TypeError (
104
+ throw new GraphQLError (
101
105
`Float cannot represent non numeric value: ${ inspect ( value ) } ` ,
102
106
) ;
103
107
}
@@ -112,8 +116,9 @@ export const GraphQLFloat = new GraphQLScalarType({
112
116
parseValue : coerceFloat ,
113
117
parseLiteral ( ast ) {
114
118
if ( ast . kind !== Kind . FLOAT && ast . kind !== Kind . INT ) {
115
- throw new TypeError (
119
+ throw new GraphQLError (
116
120
'Float cannot represent non numeric value: ' + print ( ast ) ,
121
+ ast ,
117
122
) ;
118
123
}
119
124
return parseFloat ( ast . value ) ;
@@ -153,12 +158,12 @@ function serializeString(rawValue: mixed): string {
153
158
if ( isFinite ( value ) ) {
154
159
return value . toString ( ) ;
155
160
}
156
- throw new TypeError ( `String cannot represent value: ${ inspect ( rawValue ) } ` ) ;
161
+ throw new GraphQLError ( `String cannot represent value: ${ inspect ( rawValue ) } ` ) ;
157
162
}
158
163
159
164
function coerceString ( value : mixed ) : string {
160
165
if ( typeof value !== 'string' ) {
161
- throw new TypeError (
166
+ throw new GraphQLError (
162
167
`String cannot represent a non string value: ${ inspect ( value ) } ` ,
163
168
) ;
164
169
}
@@ -173,8 +178,9 @@ export const GraphQLString = new GraphQLScalarType({
173
178
parseValue : coerceString ,
174
179
parseLiteral ( ast ) {
175
180
if ( ast . kind !== Kind . STRING ) {
176
- throw new TypeError (
181
+ throw new GraphQLError (
177
182
'String cannot represent a non string value: ' + print ( ast ) ,
183
+ ast ,
178
184
) ;
179
185
}
180
186
return ast . value ;
@@ -188,14 +194,14 @@ function serializeBoolean(value: mixed): boolean {
188
194
if ( isFinite ( value ) ) {
189
195
return value !== 0 ;
190
196
}
191
- throw new TypeError (
197
+ throw new GraphQLError (
192
198
`Boolean cannot represent a non boolean value: ${ inspect ( value ) } ` ,
193
199
) ;
194
200
}
195
201
196
202
function coerceBoolean ( value : mixed ) : boolean {
197
203
if ( typeof value !== 'boolean' ) {
198
- throw new TypeError (
204
+ throw new GraphQLError (
199
205
`Boolean cannot represent a non boolean value: ${ inspect ( value ) } ` ,
200
206
) ;
201
207
}
@@ -209,8 +215,9 @@ export const GraphQLBoolean = new GraphQLScalarType({
209
215
parseValue : coerceBoolean ,
210
216
parseLiteral ( ast ) {
211
217
if ( ast . kind !== Kind . BOOLEAN ) {
212
- throw new TypeError (
218
+ throw new GraphQLError (
213
219
'Boolean cannot represent a non boolean value: ' + print ( ast ) ,
220
+ ast ,
214
221
) ;
215
222
}
216
223
return ast . value ;
@@ -226,7 +233,7 @@ function serializeID(rawValue: mixed): string {
226
233
if ( isInteger ( value ) ) {
227
234
return String ( value ) ;
228
235
}
229
- throw new TypeError ( `ID cannot represent value: ${ inspect ( rawValue ) } ` ) ;
236
+ throw new GraphQLError ( `ID cannot represent value: ${ inspect ( rawValue ) } ` ) ;
230
237
}
231
238
232
239
function coerceID ( value : mixed ) : string {
@@ -236,7 +243,7 @@ function coerceID(value: mixed): string {
236
243
if ( isInteger ( value ) ) {
237
244
return value . toString ( ) ;
238
245
}
239
- throw new TypeError ( `ID cannot represent value: ${ inspect ( value ) } ` ) ;
246
+ throw new GraphQLError ( `ID cannot represent value: ${ inspect ( value ) } ` ) ;
240
247
}
241
248
242
249
export const GraphQLID = new GraphQLScalarType ( {
@@ -247,8 +254,9 @@ export const GraphQLID = new GraphQLScalarType({
247
254
parseValue : coerceID ,
248
255
parseLiteral ( ast ) {
249
256
if ( ast . kind !== Kind . STRING && ast . kind !== Kind . INT ) {
250
- throw new TypeError (
257
+ throw new GraphQLError (
251
258
'ID cannot represent a non-string and non-integer value: ' + print ( ast ) ,
259
+ ast ,
252
260
) ;
253
261
}
254
262
return ast . value ;
0 commit comments