Skip to content

Commit ccf6b83

Browse files
authored
Merge pull request #10 from zaeleus/graphqlerror-to-json
Remove JSON wrapper object from GraphQLError::to_json
2 parents aed9591 + bd00e6c commit ccf6b83

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/integrations/iron_handlers.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ impl<'a, CtxFactory, Query, Mutation, CtxT>
108108
let result = execute(query, None, &self.root_node, variables, &context);
109109

110110
let content_type = "application/json".parse::<Mime>().unwrap();
111+
let mut map = BTreeMap::new();
111112

112113
match result {
113114
Ok((result, errors)) => {
114-
let mut map = BTreeMap::new();
115115
map.insert("data".to_owned(), result.to_json());
116+
116117
if !errors.is_empty() {
117118
map.insert("errors".to_owned(), errors.to_json());
118119
}
@@ -124,7 +125,9 @@ impl<'a, CtxFactory, Query, Mutation, CtxT>
124125
}
125126

126127
Err(err) => {
127-
let data = err.to_json();
128+
map.insert("errors".to_owned(), err.to_json());
129+
130+
let data = Json::Object(map);
128131
let json = data.pretty();
129132

130133
Ok(Response::with((content_type, status::BadRequest, json.to_string())))

src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,20 +286,16 @@ impl<'a> From<Spanning<ParseError<'a>>> for GraphQLError<'a> {
286286

287287
impl<'a> ToJson for GraphQLError<'a> {
288288
fn to_json(&self) -> Json {
289-
let errs = match *self {
289+
match *self {
290290
GraphQLError::ParseError(ref err) => parse_error_to_json(err),
291291
GraphQLError::ValidationError(ref errs) => errs.to_json(),
292292
GraphQLError::MultipleOperationsProvided => Json::String(
293-
"Must provide operation name if query contains multiple operations.".to_owned()),
293+
"Must provide operation name if query contains multiple operations".to_owned()),
294294
GraphQLError::NoOperationProvided => Json::String(
295295
"Must provide an operation".to_owned()),
296296
GraphQLError::UnknownOperationName => Json::String(
297297
"Unknown operation".to_owned()),
298-
};
299-
300-
Json::Object(vec![
301-
("errors".to_owned(), errs),
302-
].into_iter().collect())
298+
}
303299
}
304300
}
305301

0 commit comments

Comments
 (0)