failure removed in favour of thiserror and anyhow (#696)

This commit is contained in:
Kunjan Dalal 2020-07-11 13:32:32 +05:30 committed by GitHub
parent 61cb75969e
commit 0dcfb850e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 21 deletions

View file

@ -19,7 +19,8 @@ juniper = { version = "0.14.2", path = "../juniper", default-features = false }
tokio = { version = "0.2", features = ["time"] } tokio = { version = "0.2", features = ["time"] }
serde = { version = "1.0.75", features = ["derive"] } serde = { version = "1.0.75", features = ["derive"] }
serde_json = "1.0.24" serde_json = "1.0.24"
failure = "0.1.7" anyhow = "1.0"
thiserror = "1.0"
[dev-dependencies] [dev-dependencies]
juniper = { version = "0.14.2", path = "../juniper", features = ["expose-test-schema", "serde_json"] } juniper = { version = "0.14.2", path = "../juniper", features = ["expose-test-schema", "serde_json"] }

View file

@ -13,7 +13,8 @@ subscriptions = ["juniper_subscriptions"]
[dependencies] [dependencies]
bytes = "0.5" bytes = "0.5"
failure = "0.1.7" anyhow = "1.0"
thiserror = "1.0"
futures = "0.3.1" futures = "0.3.1"
juniper = { version = "0.14.2", path = "../juniper", default-features = false } juniper = { version = "0.14.2", path = "../juniper", default-features = false }
juniper_subscriptions = { path = "../juniper_subscriptions", optional = true } juniper_subscriptions = { path = "../juniper_subscriptions", optional = true }

View file

@ -40,14 +40,14 @@ Check the LICENSE file for details.
#![deny(warnings)] #![deny(warnings)]
#![doc(html_root_url = "https://docs.rs/juniper_warp/0.2.0")] #![doc(html_root_url = "https://docs.rs/juniper_warp/0.2.0")]
use std::{collections::HashMap, str, sync::Arc}; use anyhow::anyhow;
use bytes::Bytes; use bytes::Bytes;
use futures::{FutureExt as _, TryFutureExt}; use futures::{FutureExt as _, TryFutureExt};
use juniper::{ use juniper::{
http::{GraphQLBatchRequest, GraphQLRequest}, http::{GraphQLBatchRequest, GraphQLRequest},
ScalarValue, ScalarValue,
}; };
use std::{collections::HashMap, str, sync::Arc};
use tokio::task; use tokio::task;
use warp::{body, filters::BoxedFilter, header, http, query, Filter}; use warp::{body, filters::BoxedFilter, header, http, query, Filter};
@ -153,9 +153,8 @@ where
let handle_post_graphql_request = move |context: CtxT, body: Bytes| { let handle_post_graphql_request = move |context: CtxT, body: Bytes| {
let schema = post_graphql_schema.clone(); let schema = post_graphql_schema.clone();
async move { async move {
let query = str::from_utf8(body.as_ref()).map_err(|e| { let query = str::from_utf8(body.as_ref())
failure::format_err!("Request body query is not a valid UTF-8 string: {}", e) .map_err(|e| anyhow!("Request body query is not a valid UTF-8 string: {}", e))?;
})?;
let req = GraphQLRequest::new(query.into(), None, None); let req = GraphQLRequest::new(query.into(), None, None);
let resp = req.execute(&schema, &context).await; let resp = req.execute(&schema, &context).await;
@ -177,9 +176,8 @@ where
let schema = schema.clone(); let schema = schema.clone();
async move { async move {
let req = GraphQLRequest::new( let req = GraphQLRequest::new(
qry.remove("query").ok_or_else(|| { qry.remove("query")
failure::format_err!("Missing GraphQL query string in query parameters") .ok_or_else(|| anyhow!("Missing GraphQL query string in query parameters"))?,
})?,
qry.remove("operation_name"), qry.remove("operation_name"),
qry.remove("variables") qry.remove("variables")
.map(|vs| serde_json::from_str(&vs)) .map(|vs| serde_json::from_str(&vs))
@ -247,9 +245,8 @@ where
let schema = post_graphql_schema.clone(); let schema = post_graphql_schema.clone();
async move { async move {
let res = task::spawn_blocking(move || { let res = task::spawn_blocking(move || {
let query = str::from_utf8(body.as_ref()).map_err(|e| { let query = str::from_utf8(body.as_ref())
failure::format_err!("Request body is not a valid UTF-8 string: {}", e) .map_err(|e| anyhow!("Request body is not a valid UTF-8 string: {}", e))?;
})?;
let req = GraphQLRequest::new(query.into(), None, None); let req = GraphQLRequest::new(query.into(), None, None);
let resp = req.execute_sync(&schema, &context); let resp = req.execute_sync(&schema, &context);
@ -276,7 +273,7 @@ where
let res = task::spawn_blocking(move || { let res = task::spawn_blocking(move || {
let req = GraphQLRequest::new( let req = GraphQLRequest::new(
qry.remove("query").ok_or_else(|| { qry.remove("query").ok_or_else(|| {
failure::format_err!("Missing GraphQL query string in query parameters") anyhow!("Missing GraphQL query string in query parameters")
})?, })?,
qry.remove("operation_name"), qry.remove("operation_name"),
qry.remove("variables") qry.remove("variables")
@ -314,7 +311,7 @@ pub struct JoinError(task::JoinError);
impl warp::reject::Reject for JoinError {} impl warp::reject::Reject for JoinError {}
fn build_response(response: Result<(Vec<u8>, bool), failure::Error>) -> http::Response<Vec<u8>> { fn build_response(response: Result<(Vec<u8>, bool), anyhow::Error>) -> http::Response<Vec<u8>> {
match response { match response {
Ok((body, is_ok)) => http::Response::builder() Ok((body, is_ok)) => http::Response::builder()
.status(if is_ok { 200 } else { 400 }) .status(if is_ok { 200 } else { 400 })
@ -434,7 +431,7 @@ pub mod subscriptions {
websocket: warp::ws::WebSocket, websocket: warp::ws::WebSocket,
coordinator: Arc<Coordinator<'static, Query, Mutation, Subscription, CtxT, S>>, coordinator: Arc<Coordinator<'static, Query, Mutation, Subscription, CtxT, S>>,
context: CtxT, context: CtxT,
) -> impl Future<Output = Result<(), failure::Error>> + Send ) -> impl Future<Output = Result<(), anyhow::Error>> + Send
where where
Query: juniper::GraphQLTypeAsync<S, Context = CtxT> + Send + 'static, Query: juniper::GraphQLTypeAsync<S, Context = CtxT> + Send + 'static,
Query::TypeInfo: Send + Sync, Query::TypeInfo: Send + Sync,
@ -462,7 +459,7 @@ pub mod subscriptions {
sink_rx sink_rx
.map_err(move |e| { .map_err(move |e| {
got_close_signal2.store(true, Ordering::Relaxed); got_close_signal2.store(true, Ordering::Relaxed);
failure::format_err!("Websocket error: {}", e) anyhow!("Websocket error: {}", e)
}) })
.try_fold((), move |_, msg| { .try_fold((), move |_, msg| {
let coordinator = coordinator.clone(); let coordinator = coordinator.clone();
@ -478,9 +475,9 @@ pub mod subscriptions {
let msg = msg let msg = msg
.to_str() .to_str()
.map_err(|_| failure::format_err!("Non-text messages are not accepted"))?; .map_err(|_| anyhow!("Non-text messages are not accepted"))?;
let request: WsPayload<S> = serde_json::from_str(msg) let request: WsPayload<S> = serde_json::from_str(msg)
.map_err(|e| failure::format_err!("Invalid WsPayload: {}", e))?; .map_err(|e| anyhow!("Invalid WsPayload: {}", e))?;
match request.type_name.as_str() { match request.type_name.as_str() {
"connection_init" => {} "connection_init" => {}
@ -501,10 +498,10 @@ pub mod subscriptions {
if let Some(ref payload) = request.payload { if let Some(ref payload) = request.payload {
if payload.query.is_none() { if payload.query.is_none() {
return Err(failure::format_err!("Query not found")); return Err(anyhow!("Query not found"));
} }
} else { } else {
return Err(failure::format_err!("Payload not found")); return Err(anyhow!("Payload not found"));
} }
tokio::task::spawn(async move { tokio::task::spawn(async move {