Skip to content

Commit 3d86489

Browse files
Scalars: convert errors from 'TypeError' to 'GraphQLError' (graphql#2187)
1 parent dd3b265 commit 3d86489

File tree

8 files changed

+99
-122
lines changed

8 files changed

+99
-122
lines changed

src/execution/__tests__/variables-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ describe('Execute: Handles inputs', () => {
687687
errors: [
688688
{
689689
message:
690-
'Variable "$value" got invalid value [1, 2, 3]; Expected type "String". String cannot represent a non string value: [1, 2, 3]',
690+
'Variable "$value" got invalid value [1, 2, 3]; String cannot represent a non string value: [1, 2, 3]',
691691
locations: [{ line: 2, column: 16 }],
692692
},
693693
],
@@ -1006,7 +1006,7 @@ describe('Execute: Handles inputs', () => {
10061006

10071007
function invalidValueError(value, index) {
10081008
return {
1009-
message: `Variable "$input" got invalid value ${value} at "input[${index}]"; Expected type "String". String cannot represent a non string value: ${value}`,
1009+
message: `Variable "$input" got invalid value ${value} at "input[${index}]"; String cannot represent a non string value: ${value}`,
10101010
locations: [{ line: 2, column: 14 }],
10111011
};
10121012
}

src/subscription/__tests__/subscribe-test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,6 @@ describe('Subscription Initialization Phase', () => {
519519
},
520520
};
521521

522-
// $FlowFixMe
523522
const result = await subscribe(emailSchema, ast, data, null, {
524523
priority: 'meow',
525524
});
@@ -528,13 +527,11 @@ describe('Subscription Initialization Phase', () => {
528527
errors: [
529528
{
530529
message:
531-
'Variable "$priority" got invalid value "meow"; Expected type "Int". Int cannot represent non-integer value: "meow"',
530+
'Variable "$priority" got invalid value "meow"; Int cannot represent non-integer value: "meow"',
532531
locations: [{ line: 2, column: 21 }],
533532
},
534533
],
535534
});
536-
537-
expect(result.errors[0].originalError).not.to.equal(undefined);
538535
});
539536
});
540537

src/type/__tests__/enumType-test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ describe('Type System: Enum Values', () => {
224224
expect(result).to.deep.equal({
225225
errors: [
226226
{
227-
message:
228-
'Expected value of type "Int", found GREEN; Int cannot represent non-integer value: GREEN',
227+
message: 'Int cannot represent non-integer value: GREEN',
229228
locations: [{ line: 1, column: 22 }],
230229
},
231230
],

src/type/scalars.js

+26-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import isObjectLike from '../jsutils/isObjectLike';
99
import { Kind } from '../language/kinds';
1010
import { print } from '../language/printer';
1111

12+
import { GraphQLError } from '../error/GraphQLError';
13+
1214
import { GraphQLScalarType, isScalarType } from './definition';
1315

1416
// As per the GraphQL Spec, Integers are only treated as valid when a valid
@@ -30,12 +32,12 @@ function serializeInt(value: mixed): number {
3032
}
3133

3234
if (!isInteger(num)) {
33-
throw new TypeError(
35+
throw new GraphQLError(
3436
`Int cannot represent non-integer value: ${inspect(value)}`,
3537
);
3638
}
3739
if (num > MAX_INT || num < MIN_INT) {
38-
throw new TypeError(
40+
throw new GraphQLError(
3941
`Int cannot represent non 32-bit signed integer value: ${inspect(value)}`,
4042
);
4143
}
@@ -44,12 +46,12 @@ function serializeInt(value: mixed): number {
4446

4547
function coerceInt(value: mixed): number {
4648
if (!isInteger(value)) {
47-
throw new TypeError(
49+
throw new GraphQLError(
4850
`Int cannot represent non-integer value: ${inspect(value)}`,
4951
);
5052
}
5153
if (value > MAX_INT || value < MIN_INT) {
52-
throw new TypeError(
54+
throw new GraphQLError(
5355
`Int cannot represent non 32-bit signed integer value: ${inspect(value)}`,
5456
);
5557
}
@@ -64,14 +66,16 @@ export const GraphQLInt = new GraphQLScalarType({
6466
parseValue: coerceInt,
6567
parseLiteral(ast) {
6668
if (ast.kind !== Kind.INT) {
67-
throw new TypeError(
69+
throw new GraphQLError(
6870
'Int cannot represent non-integer value: ' + print(ast),
71+
ast,
6972
);
7073
}
7174
const num = parseInt(ast.value, 10);
7275
if (num > MAX_INT || num < MIN_INT) {
73-
throw new TypeError(
76+
throw new GraphQLError(
7477
'Int cannot represent non 32-bit signed integer value: ' + ast.value,
78+
ast,
7579
);
7680
}
7781
return num;
@@ -88,7 +92,7 @@ function serializeFloat(value: mixed): number {
8892
num = Number(value);
8993
}
9094
if (!isFinite(num)) {
91-
throw new TypeError(
95+
throw new GraphQLError(
9296
`Float cannot represent non numeric value: ${inspect(value)}`,
9397
);
9498
}
@@ -97,7 +101,7 @@ function serializeFloat(value: mixed): number {
97101

98102
function coerceFloat(value: mixed): number {
99103
if (!isFinite(value)) {
100-
throw new TypeError(
104+
throw new GraphQLError(
101105
`Float cannot represent non numeric value: ${inspect(value)}`,
102106
);
103107
}
@@ -112,8 +116,9 @@ export const GraphQLFloat = new GraphQLScalarType({
112116
parseValue: coerceFloat,
113117
parseLiteral(ast) {
114118
if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.INT) {
115-
throw new TypeError(
119+
throw new GraphQLError(
116120
'Float cannot represent non numeric value: ' + print(ast),
121+
ast,
117122
);
118123
}
119124
return parseFloat(ast.value);
@@ -153,12 +158,12 @@ function serializeString(rawValue: mixed): string {
153158
if (isFinite(value)) {
154159
return value.toString();
155160
}
156-
throw new TypeError(`String cannot represent value: ${inspect(rawValue)}`);
161+
throw new GraphQLError(`String cannot represent value: ${inspect(rawValue)}`);
157162
}
158163

159164
function coerceString(value: mixed): string {
160165
if (typeof value !== 'string') {
161-
throw new TypeError(
166+
throw new GraphQLError(
162167
`String cannot represent a non string value: ${inspect(value)}`,
163168
);
164169
}
@@ -173,8 +178,9 @@ export const GraphQLString = new GraphQLScalarType({
173178
parseValue: coerceString,
174179
parseLiteral(ast) {
175180
if (ast.kind !== Kind.STRING) {
176-
throw new TypeError(
181+
throw new GraphQLError(
177182
'String cannot represent a non string value: ' + print(ast),
183+
ast,
178184
);
179185
}
180186
return ast.value;
@@ -188,14 +194,14 @@ function serializeBoolean(value: mixed): boolean {
188194
if (isFinite(value)) {
189195
return value !== 0;
190196
}
191-
throw new TypeError(
197+
throw new GraphQLError(
192198
`Boolean cannot represent a non boolean value: ${inspect(value)}`,
193199
);
194200
}
195201

196202
function coerceBoolean(value: mixed): boolean {
197203
if (typeof value !== 'boolean') {
198-
throw new TypeError(
204+
throw new GraphQLError(
199205
`Boolean cannot represent a non boolean value: ${inspect(value)}`,
200206
);
201207
}
@@ -209,8 +215,9 @@ export const GraphQLBoolean = new GraphQLScalarType({
209215
parseValue: coerceBoolean,
210216
parseLiteral(ast) {
211217
if (ast.kind !== Kind.BOOLEAN) {
212-
throw new TypeError(
218+
throw new GraphQLError(
213219
'Boolean cannot represent a non boolean value: ' + print(ast),
220+
ast,
214221
);
215222
}
216223
return ast.value;
@@ -226,7 +233,7 @@ function serializeID(rawValue: mixed): string {
226233
if (isInteger(value)) {
227234
return String(value);
228235
}
229-
throw new TypeError(`ID cannot represent value: ${inspect(rawValue)}`);
236+
throw new GraphQLError(`ID cannot represent value: ${inspect(rawValue)}`);
230237
}
231238

232239
function coerceID(value: mixed): string {
@@ -236,7 +243,7 @@ function coerceID(value: mixed): string {
236243
if (isInteger(value)) {
237244
return value.toString();
238245
}
239-
throw new TypeError(`ID cannot represent value: ${inspect(value)}`);
246+
throw new GraphQLError(`ID cannot represent value: ${inspect(value)}`);
240247
}
241248

242249
export const GraphQLID = new GraphQLScalarType({
@@ -247,8 +254,9 @@ export const GraphQLID = new GraphQLScalarType({
247254
parseValue: coerceID,
248255
parseLiteral(ast) {
249256
if (ast.kind !== Kind.STRING && ast.kind !== Kind.INT) {
250-
throw new TypeError(
257+
throw new GraphQLError(
251258
'ID cannot represent a non-string and non-integer value: ' + print(ast),
259+
ast,
252260
);
253261
}
254262
return ast.value;

src/utilities/__tests__/coerceInputValue-test.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ describe('coerceInputValue', () => {
197197
const result = coerceValue({ foo: NaN }, TestInputObject);
198198
expectErrors(result).to.deep.equal([
199199
{
200-
error:
201-
'Expected type "Int". Int cannot represent non-integer value: NaN',
200+
error: 'Int cannot represent non-integer value: NaN',
202201
path: ['foo'],
203202
value: NaN,
204203
},
@@ -209,14 +208,12 @@ describe('coerceInputValue', () => {
209208
const result = coerceValue({ foo: 'abc', bar: 'def' }, TestInputObject);
210209
expectErrors(result).to.deep.equal([
211210
{
212-
error:
213-
'Expected type "Int". Int cannot represent non-integer value: "abc"',
211+
error: 'Int cannot represent non-integer value: "abc"',
214212
path: ['foo'],
215213
value: 'abc',
216214
},
217215
{
218-
error:
219-
'Expected type "Int". Int cannot represent non-integer value: "def"',
216+
error: 'Int cannot represent non-integer value: "def"',
220217
path: ['bar'],
221218
value: 'def',
222219
},
@@ -309,14 +306,12 @@ describe('coerceInputValue', () => {
309306
const result = coerceValue([1, 'b', true, 4], TestList);
310307
expectErrors(result).to.deep.equal([
311308
{
312-
error:
313-
'Expected type "Int". Int cannot represent non-integer value: "b"',
309+
error: 'Int cannot represent non-integer value: "b"',
314310
path: [1],
315311
value: 'b',
316312
},
317313
{
318-
error:
319-
'Expected type "Int". Int cannot represent non-integer value: true',
314+
error: 'Int cannot represent non-integer value: true',
320315
path: [2],
321316
value: true,
322317
},
@@ -332,8 +327,7 @@ describe('coerceInputValue', () => {
332327
const result = coerceValue('INVALID', TestList);
333328
expectErrors(result).to.deep.equal([
334329
{
335-
error:
336-
'Expected type "Int". Int cannot represent non-integer value: "INVALID"',
330+
error: 'Int cannot represent non-integer value: "INVALID"',
337331
path: [],
338332
value: 'INVALID',
339333
},

src/utilities/coerceInputValue.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,22 @@ function coerceInputValueImpl(
166166
try {
167167
parseResult = type.parseValue(inputValue);
168168
} catch (error) {
169-
onError(
170-
pathToArray(path),
171-
inputValue,
172-
new GraphQLError(
173-
`Expected type "${type.name}". ` + error.message,
174-
undefined,
175-
undefined,
176-
undefined,
177-
undefined,
178-
error,
179-
),
180-
);
169+
if (error instanceof GraphQLError) {
170+
onError(pathToArray(path), inputValue, error);
171+
} else {
172+
onError(
173+
pathToArray(path),
174+
inputValue,
175+
new GraphQLError(
176+
`Expected type "${type.name}". ` + error.message,
177+
undefined,
178+
undefined,
179+
undefined,
180+
undefined,
181+
error,
182+
),
183+
);
184+
}
181185
return;
182186
}
183187
if (parseResult === undefined) {

0 commit comments

Comments
 (0)