Skip to content

Commit ec59766

Browse files
dvicLegNeato
authored andcommitted
Use 'extensions' as field for error details (graphql-rust#219)
1 parent 90b89f0 commit ec59766

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

changelog/master.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@
3838
* `GraphQLType` and `ToInputValue` are now implemented for Arc<T>
3939

4040
[#212](https://github.com/graphql-rust/juniper/pull/212)
41+
42+
* Error responses no longer have a *data* field, instead, error details are stored in the *extensions* field
43+
44+
**Note:** while this is a breaking change, it is a necessary one to better align with the latest [GraphQL June 2018](https://facebook.github.io/graphql/June2018/#sec-Errors) specification, which defines the reserved *extensions* field for error details.
45+
46+
[#219](https://github.com/graphql-rust/juniper/pull/219)

juniper/src/executor/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ impl Ord for ExecutionError {
127127
#[derive(Debug, PartialEq)]
128128
pub struct FieldError {
129129
message: String,
130-
data: Value,
130+
extensions: Value,
131131
}
132132

133133
impl<T: Display> From<T> for FieldError {
134134
fn from(e: T) -> FieldError {
135135
FieldError {
136136
message: format!("{}", e),
137-
data: Value::null(),
137+
extensions: Value::null(),
138138
}
139139
}
140140
}
@@ -157,26 +157,26 @@ impl FieldError {
157157
/// # fn main() { }
158158
/// ```
159159
///
160-
/// The `data` parameter will be added to the `"data"` field of the error
160+
/// The `extensions` parameter will be added to the `"extensions"` field of the error
161161
/// object in the JSON response:
162162
///
163163
/// ```json
164164
/// {
165165
/// "errors": [
166166
/// "message": "Could not open connection to the database",
167167
/// "locations": [{"line": 2, "column": 4}],
168-
/// "data": {
168+
/// "extensions": {
169169
/// "internal_error": "Connection refused"
170170
/// }
171171
/// ]
172172
/// }
173173
/// ```
174174
///
175175
/// If the argument is `Value::null()`, no extra data will be included.
176-
pub fn new<T: Display>(e: T, data: Value) -> FieldError {
176+
pub fn new<T: Display>(e: T, extensions: Value) -> FieldError {
177177
FieldError {
178178
message: format!("{}", e),
179-
data: data,
179+
extensions,
180180
}
181181
}
182182

@@ -186,8 +186,8 @@ impl FieldError {
186186
}
187187

188188
#[doc(hidden)]
189-
pub fn data(&self) -> &Value {
190-
&self.data
189+
pub fn extensions(&self) -> &Value {
190+
&self.extensions
191191
}
192192
}
193193

juniper/src/integrations/serde.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ impl ser::Serialize for ExecutionError {
3232
map.serialize_key("path")?;
3333
map.serialize_value(self.path())?;
3434

35-
if !self.error().data().is_null() {
36-
map.serialize_key("data")?;
37-
map.serialize_value(self.error().data())?;
35+
if !self.error().extensions().is_null() {
36+
map.serialize_key("extensions")?;
37+
map.serialize_value(self.error().extensions())?;
3838
}
3939

4040
map.end()
@@ -285,10 +285,11 @@ impl ser::Serialize for Value {
285285

286286
#[cfg(test)]
287287
mod tests {
288-
use super::GraphQLError;
288+
use super::{ExecutionError, GraphQLError};
289289
use ast::InputValue;
290290
use serde_json::from_str;
291291
use serde_json::to_string;
292+
use {FieldError, Value};
292293

293294
#[test]
294295
fn int() {
@@ -318,4 +319,15 @@ mod tests {
318319
r#"[{"message":"Unknown operation"}]"#
319320
);
320321
}
322+
323+
#[test]
324+
fn error_extensions() {
325+
assert_eq!(
326+
to_string(&ExecutionError::at_origin(FieldError::new(
327+
"foo error",
328+
Value::Object(indexmap!{"foo".to_string() => Value::String("bar".to_string())}),
329+
))).unwrap(),
330+
r#"{"message":"foo error","locations":[{"line":1,"column":1}],"path":[],"extensions":{"foo":"bar"}}"#
331+
);
332+
}
321333
}

juniper/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ extern crate serde_derive;
9898
extern crate serde_json;
9999

100100
extern crate fnv;
101+
102+
#[cfg_attr(test, macro_use)]
101103
extern crate indexmap;
102104

103105
#[cfg(any(test, feature = "chrono"))]

0 commit comments

Comments
 (0)