Skip to content

Use 'extensions' as field for error details #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/master.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@
* `GraphQLType` and `ToInputValue` are now implemented for Arc<T>

[#212](https://github.com/graphql-rust/juniper/pull/212)

* Error responses no longer have a *data* field, instead, error details are stored in the *extensions* field

**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.

[#219](https://github.com/graphql-rust/juniper/pull/219)
16 changes: 8 additions & 8 deletions juniper/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ impl Ord for ExecutionError {
#[derive(Debug, PartialEq)]
pub struct FieldError {
message: String,
data: Value,
extensions: Value,
}

impl<T: Display> From<T> for FieldError {
fn from(e: T) -> FieldError {
FieldError {
message: format!("{}", e),
data: Value::null(),
extensions: Value::null(),
}
}
}
Expand All @@ -157,26 +157,26 @@ impl FieldError {
/// # fn main() { }
/// ```
///
/// The `data` parameter will be added to the `"data"` field of the error
/// The `extensions` parameter will be added to the `"extensions"` field of the error
/// object in the JSON response:
///
/// ```json
/// {
/// "errors": [
/// "message": "Could not open connection to the database",
/// "locations": [{"line": 2, "column": 4}],
/// "data": {
/// "extensions": {
/// "internal_error": "Connection refused"
/// }
/// ]
/// }
/// ```
///
/// If the argument is `Value::null()`, no extra data will be included.
pub fn new<T: Display>(e: T, data: Value) -> FieldError {
pub fn new<T: Display>(e: T, extensions: Value) -> FieldError {
FieldError {
message: format!("{}", e),
data: data,
extensions,
}
}

Expand All @@ -186,8 +186,8 @@ impl FieldError {
}

#[doc(hidden)]
pub fn data(&self) -> &Value {
&self.data
pub fn extensions(&self) -> &Value {
&self.extensions
}
}

Expand Down
20 changes: 16 additions & 4 deletions juniper/src/integrations/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ impl ser::Serialize for ExecutionError {
map.serialize_key("path")?;
map.serialize_value(self.path())?;

if !self.error().data().is_null() {
map.serialize_key("data")?;
map.serialize_value(self.error().data())?;
if !self.error().extensions().is_null() {
map.serialize_key("extensions")?;
map.serialize_value(self.error().extensions())?;
}

map.end()
Expand Down Expand Up @@ -269,10 +269,11 @@ impl ser::Serialize for Value {

#[cfg(test)]
mod tests {
use super::GraphQLError;
use super::{ExecutionError, GraphQLError};
use ast::InputValue;
use serde_json::from_str;
use serde_json::to_string;
use {FieldError, Value};

#[test]
fn int() {
Expand Down Expand Up @@ -302,4 +303,15 @@ mod tests {
r#"[{"message":"Unknown operation"}]"#
);
}

#[test]
fn error_extensions() {
assert_eq!(
to_string(&ExecutionError::at_origin(FieldError::new(
"foo error",
Value::Object(indexmap!{"foo".to_string() => Value::String("bar".to_string())}),
))).unwrap(),
r#"{"message":"foo error","locations":[{"line":1,"column":1}],"path":[],"extensions":{"foo":"bar"}}"#
);
}
}
2 changes: 2 additions & 0 deletions juniper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ extern crate serde_derive;
extern crate serde_json;

extern crate fnv;

#[cfg_attr(test, macro_use)]
extern crate indexmap;

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