forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonNullableFieldWasNullException.java
More file actions
67 lines (53 loc) · 1.98 KB
/
Copy pathNonNullableFieldWasNullException.java
File metadata and controls
67 lines (53 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package graphql.execution;
import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.language.SourceLocation;
import java.util.List;
import static graphql.Assert.assertNotNull;
/**
* See (http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability), but if a non nullable field
* actually resolves to a null value and the parent type is nullable then the parent must in fact become null
* so we use exceptions to indicate this special case
*/
public class NonNullableFieldWasNullException extends RuntimeException implements GraphQLError {
private final TypeInfo typeInfo;
private final ExecutionPath path;
public NonNullableFieldWasNullException(TypeInfo typeInfo, ExecutionPath path) {
super(buildMsg(assertNotNull(typeInfo), assertNotNull(path)));
this.typeInfo = typeInfo;
this.path = path;
}
private static String buildMsg(TypeInfo typeInfo, ExecutionPath path) {
if (typeInfo.hasParentType()) {
return String.format("Cannot return null for non-nullable type: '%s' within parent '%s' (%s)", typeInfo.type().getName(), typeInfo.parentTypeInfo().type().getName(), path);
}
return String.format("Cannot return null for non-nullable type: '%s' (%s) ", typeInfo.type().getName(), path);
}
public TypeInfo getTypeInfo() {
return typeInfo;
}
@Override
public List<SourceLocation> getLocations() {
return null;
}
@Override
public ErrorType getErrorType() {
return null;
}
/**
* The graphql spec says that that path field of any error should be a list
* of path entries - http://facebook.github.io/graphql/#sec-Errors
*
* @return the path in list format
*/
public List<Object> getPath() {
return path.toList();
}
@Override
public String toString() {
return "NonNullableFieldWasNullException{" +
"path=" + path +
"typeInfo=" + typeInfo +
'}';
}
}