Skip to content

Commit 29fdba2

Browse files
Drew Olsonbbakerman
authored andcommitted
Handle null types when displaying type names
1 parent f670432 commit 29fdba2

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/main/java/graphql/Scalars.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ private static boolean isNumberIsh(Object input) {
3838
return input instanceof Number || input instanceof String;
3939
}
4040

41+
private static String typeName(Object input) {
42+
if (input == null) {
43+
return "null";
44+
}
45+
46+
return input.getClass().getSimpleName();
47+
}
48+
4149
/**
4250
* This represents the "Int" type as defined in the graphql specification : http://facebook.github.io/graphql/#sec-Int
4351
*
@@ -70,7 +78,7 @@ public Integer serialize(Object input) {
7078
Integer result = convertImpl(input);
7179
if (result == null) {
7280
throw new CoercingSerializeException(
73-
"Expected type 'Int' but was '" + input.getClass().getSimpleName() + "'."
81+
"Expected type 'Int' but was '" + typeName(input) + "'."
7482
);
7583
}
7684
return result;
@@ -81,7 +89,7 @@ public Integer parseValue(Object input) {
8189
Integer result = convertImpl(input);
8290
if (result == null) {
8391
throw new CoercingParseValueException(
84-
"Expected type 'Int' but was '" + input.getClass().getSimpleName() + "'."
92+
"Expected type 'Int' but was '" + typeName(input) + "'."
8593
);
8694
}
8795
return result;
@@ -125,7 +133,7 @@ public Double serialize(Object input) {
125133
Double result = convertImpl(input);
126134
if (result == null) {
127135
throw new CoercingSerializeException(
128-
"Expected type 'Float' but was '" + input.getClass().getSimpleName() + "'."
136+
"Expected type 'Float' but was '" + typeName(input) + "'."
129137
);
130138
}
131139
return result;
@@ -137,7 +145,7 @@ public Double parseValue(Object input) {
137145
Double result = convertImpl(input);
138146
if (result == null) {
139147
throw new CoercingParseValueException(
140-
"Expected type 'Float' but was '" + input.getClass().getSimpleName() + "'."
148+
"Expected type 'Float' but was '" + typeName(input) + "'."
141149
);
142150
}
143151
return result;
@@ -206,7 +214,7 @@ public Boolean serialize(Object input) {
206214
Boolean result = convertImpl(input);
207215
if (result == null) {
208216
throw new CoercingSerializeException(
209-
"Expected type 'Boolean' but was '" + input.getClass().getSimpleName() + "'."
217+
"Expected type 'Boolean' but was '" + typeName(input) + "'."
210218
);
211219
}
212220
return result;
@@ -217,7 +225,7 @@ public Boolean parseValue(Object input) {
217225
Boolean result = convertImpl(input);
218226
if (result == null) {
219227
throw new CoercingParseValueException(
220-
"Expected type 'Boolean' but was '" + input.getClass().getSimpleName() + "'."
228+
"Expected type 'Boolean' but was '" + typeName(input) + "'."
221229
);
222230
}
223231
return result;
@@ -258,7 +266,7 @@ public String serialize(Object input) {
258266
String result = convertImpl(input);
259267
if (result == null) {
260268
throw new CoercingSerializeException(
261-
"Expected type 'ID' but was '" + input.getClass().getSimpleName() + "'."
269+
"Expected type 'ID' but was '" + typeName(input) + "'."
262270
);
263271
}
264272
return result;
@@ -269,7 +277,7 @@ public String parseValue(Object input) {
269277
String result = convertImpl(input);
270278
if (result == null) {
271279
throw new CoercingParseValueException(
272-
"Expected type 'ID' but was '" + input.getClass().getSimpleName() + "'."
280+
"Expected type 'ID' but was '" + typeName(input) + "'."
273281
);
274282
}
275283
return result;
@@ -318,7 +326,7 @@ public Long serialize(Object input) {
318326
Long result = convertImpl(input);
319327
if (result == null) {
320328
throw new CoercingSerializeException(
321-
"Expected type 'Long' but was '" + input.getClass().getSimpleName() + "'."
329+
"Expected type 'Long' but was '" + typeName(input) + "'."
322330
);
323331
}
324332
return result;
@@ -329,7 +337,7 @@ public Long parseValue(Object input) {
329337
Long result = convertImpl(input);
330338
if (result == null) {
331339
throw new CoercingParseValueException(
332-
"Expected type 'Long' but was '" + input.getClass().getSimpleName() + "'."
340+
"Expected type 'Long' but was '" + typeName(input) + "'."
333341
);
334342
}
335343
return result;
@@ -385,7 +393,7 @@ public Short serialize(Object input) {
385393
Short result = convertImpl(input);
386394
if (result == null) {
387395
throw new CoercingSerializeException(
388-
"Expected type 'Short' but was '" + input.getClass().getSimpleName() + "'."
396+
"Expected type 'Short' but was '" + typeName(input) + "'."
389397
);
390398
}
391399
return result;
@@ -396,7 +404,7 @@ public Short parseValue(Object input) {
396404
Short result = convertImpl(input);
397405
if (result == null) {
398406
throw new CoercingParseValueException(
399-
"Expected type 'Short' but was '" + input.getClass().getSimpleName() + "'."
407+
"Expected type 'Short' but was '" + typeName(input) + "'."
400408
);
401409
}
402410
return result;
@@ -444,7 +452,7 @@ public Byte serialize(Object input) {
444452
Byte result = convertImpl(input);
445453
if (result == null) {
446454
throw new CoercingSerializeException(
447-
"Expected type 'Byte' but was '" + input.getClass().getSimpleName() + "'."
455+
"Expected type 'Byte' but was '" + typeName(input) + "'."
448456
);
449457
}
450458
return result;
@@ -455,7 +463,7 @@ public Byte parseValue(Object input) {
455463
Byte result = convertImpl(input);
456464
if (result == null) {
457465
throw new CoercingParseValueException(
458-
"Expected type 'Byte' but was '" + input.getClass().getSimpleName() + "'."
466+
"Expected type 'Byte' but was '" + typeName(input) + "'."
459467
);
460468
}
461469
return result;
@@ -501,7 +509,7 @@ public BigInteger serialize(Object input) {
501509
BigInteger result = convertImpl(input);
502510
if (result == null) {
503511
throw new CoercingSerializeException(
504-
"Expected type 'BigInteger' but was '" + input.getClass().getSimpleName() + "'."
512+
"Expected type 'BigInteger' but was '" + typeName(input) + "'."
505513
);
506514
}
507515
return result;
@@ -512,7 +520,7 @@ public BigInteger parseValue(Object input) {
512520
BigInteger result = convertImpl(input);
513521
if (result == null) {
514522
throw new CoercingParseValueException(
515-
"Expected type 'BigInteger' but was '" + input.getClass().getSimpleName() + "'."
523+
"Expected type 'BigInteger' but was '" + typeName(input) + "'."
516524
);
517525
}
518526
return result;
@@ -561,7 +569,7 @@ public BigDecimal serialize(Object input) {
561569
BigDecimal result = convertImpl(input);
562570
if (result == null) {
563571
throw new CoercingSerializeException(
564-
"Expected type 'BigDecimal' but was '" + input.getClass().getSimpleName() + "'."
572+
"Expected type 'BigDecimal' but was '" + typeName(input) + "'."
565573
);
566574
}
567575
return result;
@@ -572,7 +580,7 @@ public BigDecimal parseValue(Object input) {
572580
BigDecimal result = convertImpl(input);
573581
if (result == null) {
574582
throw new CoercingParseValueException(
575-
"Expected type 'BigDecimal' but was '" + input.getClass().getSimpleName() + "'."
583+
"Expected type 'BigDecimal' but was '" + typeName(input) + "'."
576584
);
577585
}
578586
return result;
@@ -617,7 +625,7 @@ public Character serialize(Object input) {
617625
Character result = convertImpl(input);
618626
if (result == null) {
619627
throw new CoercingSerializeException(
620-
"Expected type 'Char' but was '" + input.getClass().getSimpleName() + "'."
628+
"Expected type 'Char' but was '" + typeName(input) + "'."
621629
);
622630
}
623631
return result;
@@ -628,7 +636,7 @@ public Character parseValue(Object input) {
628636
Character result = convertImpl(input);
629637
if (result == null) {
630638
throw new CoercingParseValueException(
631-
"Expected type 'Char' but was '" + input.getClass().getSimpleName() + "'."
639+
"Expected type 'Char' but was '" + typeName(input) + "'."
632640
);
633641
}
634642
return result;

0 commit comments

Comments
 (0)