Remove ResultExt and jtry! macro

This commit is contained in:
theduke 2017-12-03 18:33:01 +01:00
parent eb70d29e04
commit 0f916f4aa6
4 changed files with 14 additions and 78 deletions

View file

@ -1,4 +1,5 @@
# [0.9.0] 2017-12-03
## [0.9.0] 2017-12-03
## Changes ## Changes
@ -26,11 +27,16 @@ The iron and rocket integrations were removed from the main crate, and are now
available via the [juniper_iron](https://crates.io/crates/juniper_iron) and available via the [juniper_iron](https://crates.io/crates/juniper_iron) and
[juniper_rocket](https://crates.io/crates/juniper_rocket) crates. [juniper_rocket](https://crates.io/crates/juniper_rocket) crates.
### FieldError - Custom data ### FieldError rewrite (custom data)
The `FieldError` type now supports custom data with the `Value` type from The `FieldError` type now supports custom data with the `Value` type from
serde_json. Use this to populate the `data` field in returned errors. serde_json. Use this to populate the `data` field in returned errors.
This also means that try! and `?` now work in resolvers, which is quite nice.
Also, the `ResultExt` extension and the `jtry!` macro were removed, since they
are redundant now!
### Dynamic Schemas ### Dynamic Schemas
Juniper has gained support for dynamic schemas, thanks to @srijs. Juniper has gained support for dynamic schemas, thanks to @srijs.

View file

@ -216,7 +216,6 @@ mod dynamic_context_switching {
use schema::model::RootNode; use schema::model::RootNode;
use parser::SourcePosition; use parser::SourcePosition;
use executor::{Context, ExecutionError, FieldError, FieldResult}; use executor::{Context, ExecutionError, FieldError, FieldResult};
use result_ext::ResultExt;
struct Schema; struct Schema;
@ -239,19 +238,18 @@ mod dynamic_context_switching {
} }
field item_res(&executor, key: i32) -> FieldResult<(&InnerContext, ItemRef)> { field item_res(&executor, key: i32) -> FieldResult<(&InnerContext, ItemRef)> {
executor.context().items.get(&key) let res = executor.context().items.get(&key)
.ok_or(format!("Could not find key {}", key)) .ok_or(format!("Could not find key {}", key))
.map(|c| (c, ItemRef)) .map(|c| (c, ItemRef))?;
.to_field_result() Ok(res)
} }
field item_res_opt(&executor, key: i32) -> FieldResult<Option<(&InnerContext, ItemRef)>> { field item_res_opt(&executor, key: i32) -> FieldResult<Option<(&InnerContext, ItemRef)>> {
if key > 100 { if key > 100 {
Err(format!("Key too large: {}", key)).to_field_result() Err(format!("Key too large: {}", key))?;
} else {
Ok(executor.context().items.get(&key)
.map(|c| (c, ItemRef)))
} }
Ok(executor.context().items.get(&key)
.map(|c| (c, ItemRef)))
} }
field item_always(&executor, key: i32) -> (&InnerContext, ItemRef) { field item_always(&executor, key: i32) -> (&InnerContext, ItemRef) {

View file

@ -133,8 +133,6 @@ mod executor;
pub mod integrations; pub mod integrations;
pub mod graphiql; pub mod graphiql;
pub mod http; pub mod http;
#[macro_use]
mod result_ext;
#[cfg(all(test, not(feature = "expose-test-schema")))] #[cfg(all(test, not(feature = "expose-test-schema")))]
mod tests; mod tests;
@ -159,7 +157,6 @@ pub use executor::{Context, ExecutionError, ExecutionResult, Executor, FieldErro
pub use validation::RuleError; pub use validation::RuleError;
pub use types::scalars::{EmptyMutation, ID}; pub use types::scalars::{EmptyMutation, ID};
pub use schema::model::RootNode; pub use schema::model::RootNode;
pub use result_ext::ResultExt;
pub use schema::meta; pub use schema::meta;

View file

@ -1,65 +0,0 @@
use std::fmt;
use std::result::Result;
use FieldError;
/**
Helper trait to produce `FieldResult`s
`FieldResult` only have strings as errors as that's what's going out
in the GraphQL response. As such, all errors must be manually
converted to strings. Importing the `ResultExt` macro and using its
only method `to_field_result` can help with that:
```rust
use std::str::FromStr;
use juniper::{FieldResult, ResultExt};
fn sample_fn(s: &str) -> FieldResult<i32> {
i32::from_str(s).to_field_result()
}
# fn main() { assert_eq!(sample_fn("12"), Ok(12)); }
```
Alternatively, you can use the `jtry!` macro in all places you'd
normally use the regular `try!` macro:
```rust
#[macro_use] extern crate juniper;
use std::str::FromStr;
use juniper::{FieldResult, ResultExt};
fn sample_fn(s: &str) -> FieldResult<i32> {
let value = jtry!(i32::from_str(s));
Ok(value)
}
# fn main() { assert_eq!(sample_fn("12"), Ok(12)); }
```
*/
pub trait ResultExt<T, E: fmt::Display> {
/// Convert the error to a string by using it's `Display` implementation
fn to_field_result(self) -> Result<T, FieldError>;
}
impl<T, E: fmt::Display> ResultExt<T, E> for Result<T, E> {
fn to_field_result(self) -> Result<T, FieldError> {
self.map_err(|e| FieldError::from(e))
}
}
/**
Helper macro to produce `FieldResult`s.
See the documentation for the [`ResultExt`](trait.ResultExt.html)
trait.
*/
#[macro_export]
macro_rules! jtry {
( $e:expr ) => { try!($crate::ResultExt::to_field_result($e)) }
}