You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -41,6 +50,117 @@ there - those errors are automatically converted into `FieldError`.
41
50
42
51
When a field returns an error, the field's result is replaced by `null`, an
43
52
additional `errors` object is created at the top level of the response, and the
44
-
execution is resumed. If an error is returned from a non-null field, such as the
53
+
execution is resumed. For example, with the previous example and the following
54
+
query:
55
+
56
+
```graphql
57
+
{
58
+
example {
59
+
contents
60
+
foo
61
+
}
62
+
}
63
+
```
64
+
65
+
If `str::from_utf8` resulted in a `std::str::Utf8Error`, the following would be
66
+
returned:
67
+
68
+
!FILENAME Response for nullable field with error
69
+
70
+
```js
71
+
{
72
+
"data": {
73
+
"example": {
74
+
contents:"<Contents of the file>",
75
+
foo:null,
76
+
}
77
+
},
78
+
"errors": [
79
+
"message":"invalid utf-8 sequence of 2 bytes from index 0",
80
+
"locations": [{ "line":2, "column":4 }])
81
+
]
82
+
}
83
+
```
84
+
85
+
If an error is returned from a non-null field, such as the
45
86
example above, the `null` value is propagated up to the first nullable parent
46
87
field, or the root `data` object if there are no nullable fields.
88
+
89
+
For example, with the following query:
90
+
91
+
```graphql
92
+
{
93
+
example {
94
+
contents
95
+
}
96
+
}
97
+
```
98
+
99
+
If `File::open()` above resulted in `std::io::ErrorKind::PermissionDenied`, the
100
+
following would be returned:
101
+
102
+
!FILENAME Response for non-null field with error and no nullable parent
103
+
104
+
```js
105
+
{
106
+
"errors": [
107
+
"message":"Permission denied (os error 13)",
108
+
"locations": [{ "line":2, "column":4 }])
109
+
]
110
+
}
111
+
```
112
+
113
+
## Structured errors
114
+
115
+
Sometimes it may be desirable to include structured error information to
116
+
clients. This can be accomplished by implementing [`IntoFieldError`](https://docs.rs/juniper/latest/juniper/trait.IntoFieldError.html), as the following example illustrates:
117
+
118
+
```rust
119
+
# #[macro_use] externcrate juniper;
120
+
usejuniper::{FieldError, IntoFieldError};
121
+
122
+
enumCustomError {
123
+
WhateverNotSet,
124
+
}
125
+
126
+
implIntoFieldErrorforCustomError {
127
+
fninto_field_error(self) ->FieldError {
128
+
matchself {
129
+
CustomError::WhateverNotSet=>FieldError::new(
130
+
"Whatever does not exist",
131
+
graphql_value!({
132
+
"type":"NO_WHATEVER"
133
+
}),
134
+
),
135
+
}
136
+
}
137
+
}
138
+
139
+
structExample {
140
+
whatever:Option<bool>,
141
+
}
142
+
143
+
graphql_object!(Example: () |&self| {
144
+
fieldwhatever() ->Result<bool, CustomError> {
145
+
ifletSome(value) =self.whatever {
146
+
returnvalue;
147
+
}
148
+
Err(CustomError::WhateverNotSet)
149
+
});
150
+
151
+
# fnmain() {}
152
+
```
153
+
154
+
The structured error information is included in the [`extensions`](https://facebook.github.io/graphql/June2018/#sec-Errors) key:
0 commit comments