diff --git a/README.md b/README.md index c4a599a1e..ac6353d08 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ A typed GraphQL client library for Rust. )] pub struct UnionQuery; - fn perform_my_query(variables: union_query::Variables) -> Result<(), failure::Error> { + fn perform_my_query(variables: union_query::Variables) -> Result<(), anyhow::Error> { // this is the important line let request_body = UnionQuery::build_query(variables); diff --git a/examples/github/Cargo.toml b/examples/github/Cargo.toml index 331e26165..ca95d2604 100644 --- a/examples/github/Cargo.toml +++ b/examples/github/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Tom Houlé "] edition = "2018" [dev-dependencies] -failure = "*" +anyhow = "*" graphql_client = { path = "../../graphql_client" } serde = "^1.0" reqwest = "^0.9" diff --git a/examples/github/examples/github.rs b/examples/github/examples/github.rs index 4603bc131..fe6708b1e 100644 --- a/examples/github/examples/github.rs +++ b/examples/github/examples/github.rs @@ -1,4 +1,4 @@ -use failure::*; +use anyhow::*; use graphql_client::*; use log::*; use prettytable::*; @@ -27,7 +27,7 @@ struct Env { github_api_token: String, } -fn parse_repo_name(repo_name: &str) -> Result<(&str, &str), failure::Error> { +fn parse_repo_name(repo_name: &str) -> Result<(&str, &str), anyhow::Error> { let mut parts = repo_name.split('/'); match (parts.next(), parts.next()) { (Some(owner), Some(name)) => Ok((owner, name)), @@ -35,7 +35,7 @@ fn parse_repo_name(repo_name: &str) -> Result<(&str, &str), failure::Error> { } } -fn main() -> Result<(), failure::Error> { +fn main() -> Result<(), anyhow::Error> { dotenv::dotenv().ok(); env_logger::init(); diff --git a/examples/hasura/Cargo.toml b/examples/hasura/Cargo.toml index d8625dc3e..fd88c28b1 100644 --- a/examples/hasura/Cargo.toml +++ b/examples/hasura/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Mark Catley "] edition = "2018" [dev-dependencies] -failure = "*" +anyhow = "*" graphql_client = { path = "../../graphql_client" } serde = "1.0" serde_derive = "1.0" diff --git a/examples/hasura/examples/hasura.rs b/examples/hasura/examples/hasura.rs index 64a619a18..8c8072261 100644 --- a/examples/hasura/examples/hasura.rs +++ b/examples/hasura/examples/hasura.rs @@ -14,7 +14,7 @@ type Timestamptz = String; )] struct UpsertIssue; -fn main() -> Result<(), failure::Error> { +fn main() -> Result<(), anyhow::Error> { use upsert_issue::{IssuesUpdateColumn::*, *}; dotenv::dotenv().ok(); env_logger::init(); diff --git a/graphql_client/Cargo.toml b/graphql_client/Cargo.toml index 570e987c0..282c6db63 100644 --- a/graphql_client/Cargo.toml +++ b/graphql_client/Cargo.toml @@ -11,7 +11,8 @@ edition = "2018" [dependencies] doc-comment = "^0.3" -failure = { version = "0.1", optional = true } +anyhow = { version = "1.0", optional = true } +thiserror = { version = "1.0", optional = true } graphql_query_derive = { path = "../graphql_query_derive", version = "0.9.0" } serde_json = "1.0" serde = { version = "^1.0.78", features = ["derive"] } @@ -56,7 +57,8 @@ wasm-bindgen-test = "^0.2" [features] web = [ - "failure", + "anyhow", + "thiserror", "futures", "js-sys", "log", diff --git a/graphql_client/src/lib.rs b/graphql_client/src/lib.rs index b64b3df2b..c83a750b1 100644 --- a/graphql_client/src/lib.rs +++ b/graphql_client/src/lib.rs @@ -39,7 +39,7 @@ doc_comment::doctest!("../../README.md"); /// )] /// struct StarWarsQuery; /// -/// fn main() -> Result<(), failure::Error> { +/// fn main() -> Result<(), anyhow::Error> { /// use graphql_client::GraphQLQuery; /// /// let variables = star_wars_query::Variables { @@ -130,7 +130,7 @@ impl Display for PathFragment { /// # something: i32 /// # } /// # -/// # fn main() -> Result<(), failure::Error> { +/// # fn main() -> Result<(), anyhow::Error> { /// use graphql_client::*; /// /// let body: Response = serde_json::from_value(json!({ @@ -247,7 +247,7 @@ impl Display for Error { /// # dogs: Vec, /// # } /// # -/// # fn main() -> Result<(), failure::Error> { +/// # fn main() -> Result<(), anyhow::Error> { /// use graphql_client::Response; /// /// let body: Response = serde_json::from_value(json!({ diff --git a/graphql_client/src/web.rs b/graphql_client/src/web.rs index 00a6a3521..8f4b6031b 100644 --- a/graphql_client/src/web.rs +++ b/graphql_client/src/web.rs @@ -2,10 +2,10 @@ //! [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen). use crate::*; -use failure::*; use futures::{Future, IntoFuture}; use log::*; use std::collections::HashMap; +use thiserror::*; use wasm_bindgen::{JsCast, JsValue}; use wasm_bindgen_futures::JsFuture; @@ -24,33 +24,33 @@ pub struct Client { /// All the ways a request can go wrong. /// /// not exhaustive -#[derive(Debug, Fail, PartialEq)] +#[derive(Debug, Error, PartialEq)] pub enum ClientError { /// The body couldn't be built - #[fail(display = "Request body is not a valid string")] + #[error("Request body is not a valid string")] Body, /// An error caused by window.fetch - #[fail(display = "Network error")] + #[error("Network error")] Network(String), /// Error in a dynamic JS cast that should have worked - #[fail(display = "JS casting error")] + #[error("JS casting error")] Cast, /// No window object could be retrieved - #[fail( - display = "No Window object available - the client works only in a browser (non-worker) context" + #[error( + "No Window object available - the client works only in a browser (non-worker) context" )] NoWindow, /// Response shape does not match the generated code - #[fail(display = "Response shape error")] + #[error("Response shape error")] ResponseShape, /// Response could not be converted to text - #[fail(display = "Response conversion to text failed (Response.text threw)")] + #[error("Response conversion to text failed (Response.text threw)")] ResponseText, /// Exception thrown when building the request - #[fail(display = "Error building the request")] + #[error("Error building the request")] RequestError, /// Other JS exception - #[fail(display = "Unexpected JS exception")] + #[error("Unexpected JS exception")] JsException, } diff --git a/graphql_client_cli/Cargo.toml b/graphql_client_cli/Cargo.toml index 4b4ab1682..7857e0b25 100644 --- a/graphql_client_cli/Cargo.toml +++ b/graphql_client_cli/Cargo.toml @@ -12,7 +12,7 @@ name = "graphql-client" path = "src/main.rs" [dependencies] -failure = "^0.1" +anyhow = "1.0" reqwest = "^0.9" graphql_client = { version = "0.9.0", path = "../graphql_client" } graphql_client_codegen = { path = "../graphql_client_codegen/", version = "0.9.0" } diff --git a/graphql_client_cli/src/generate.rs b/graphql_client_cli/src/generate.rs index ca0a66f4d..f32fa0cc7 100644 --- a/graphql_client_cli/src/generate.rs +++ b/graphql_client_cli/src/generate.rs @@ -1,4 +1,4 @@ -use failure::*; +use anyhow::*; use graphql_client_codegen::{ generate_module_token_stream, CodegenMode, GraphQLClientCodegenOptions, }; @@ -19,7 +19,7 @@ pub(crate) struct CliCodegenParams { pub output_directory: Option, } -pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Error> { +pub(crate) fn generate_code(params: CliCodegenParams) -> Result<()> { let CliCodegenParams { variables_derives, response_derives, @@ -59,7 +59,7 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Err options.set_deprecation_strategy(deprecation_strategy); } - let gen = generate_module_token_stream(query_path.clone(), &schema_path, options)?; + let gen = generate_module_token_stream(query_path.clone(), &schema_path, options).map_err(|fail| fail.compat())?; let generated_code = gen.to_string(); let generated_code = if cfg!(feature = "rustfmt") && !no_formatting { diff --git a/graphql_client_cli/src/introspect_schema.rs b/graphql_client_cli/src/introspect_schema.rs index 1d8cbe721..4650e05f1 100644 --- a/graphql_client_cli/src/introspect_schema.rs +++ b/graphql_client_cli/src/introspect_schema.rs @@ -1,4 +1,4 @@ -use failure::format_err; +use anyhow::format_err; use graphql_client::GraphQLQuery; use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE}; use std::path::PathBuf; @@ -18,7 +18,7 @@ pub fn introspect_schema( output: Option, authorization: Option, headers: Vec
, -) -> Result<(), failure::Error> { +) -> anyhow::Result<()> { use std::io::Write; let out: Box = match output { @@ -73,7 +73,7 @@ pub struct Header { } impl FromStr for Header { - type Err = failure::Error; + type Err = anyhow::Error; fn from_str(input: &str) -> Result { // error: colon required for name/value pair diff --git a/graphql_client_cli/src/main.rs b/graphql_client_cli/src/main.rs index 4da37c36a..ff6435c73 100644 --- a/graphql_client_cli/src/main.rs +++ b/graphql_client_cli/src/main.rs @@ -69,7 +69,7 @@ enum Cli { }, } -fn main() -> Result<(), failure::Error> { +fn main() -> anyhow::Result<()> { set_env_logger(); let cli = Cli::from_args(); diff --git a/graphql_query_derive/Cargo.toml b/graphql_query_derive/Cargo.toml index 092dcd583..2d8df8156 100644 --- a/graphql_query_derive/Cargo.toml +++ b/graphql_query_derive/Cargo.toml @@ -11,7 +11,7 @@ edition = "2018" proc-macro = true [dependencies] -failure = "^0.1" +anyhow = "1.0" syn = { version = "^1.0", features = ["extra-traits"] } proc-macro2 = { version = "^1.0", features = [] } graphql_client_codegen = { path = "../graphql_client_codegen/", version = "0.9.0" } diff --git a/graphql_query_derive/src/attributes.rs b/graphql_query_derive/src/attributes.rs index c25513d83..3512e20e0 100644 --- a/graphql_query_derive/src/attributes.rs +++ b/graphql_query_derive/src/attributes.rs @@ -1,4 +1,4 @@ -use failure::*; +use anyhow::*; use graphql_client_codegen::deprecation::DeprecationStrategy; use graphql_client_codegen::normalization::Normalization; use syn; @@ -12,7 +12,7 @@ fn path_to_match() -> syn::Path { } /// Extract an configuration parameter specified in the `graphql` attribute. -pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result { +pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result { let attributes = &ast.attrs; let graphql_path = path_to_match(); let attribute = attributes @@ -38,9 +38,7 @@ pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result Result { +pub fn extract_deprecation_strategy(ast: &syn::DeriveInput) -> Result { extract_attr(&ast, "deprecated")? .to_lowercase() .as_str() @@ -49,7 +47,7 @@ pub fn extract_deprecation_strategy( } /// Get the deprecation from a struct attribute in the derive case. -pub fn extract_normalization(ast: &syn::DeriveInput) -> Result { +pub fn extract_normalization(ast: &syn::DeriveInput) -> Result { extract_attr(&ast, "normalization")? .to_lowercase() .as_str() diff --git a/graphql_query_derive/src/lib.rs b/graphql_query_derive/src/lib.rs index 6cf96eb9e..2d3bdae01 100644 --- a/graphql_query_derive/src/lib.rs +++ b/graphql_query_derive/src/lib.rs @@ -3,7 +3,7 @@ extern crate proc_macro; /// Derive-related code. This will be moved into graphql_query_derive. mod attributes; -use failure::ResultExt; +use anyhow::Context; use graphql_client_codegen::{ generate_module_token_stream, CodegenMode, GraphQLClientCodegenOptions, }; @@ -15,21 +15,13 @@ use proc_macro2::TokenStream; pub fn derive_graphql_query(input: proc_macro::TokenStream) -> proc_macro::TokenStream { match graphql_query_derive_inner(input) { Ok(ts) => ts, - Err(err) => panic!( - "{}", - err.iter_chain() - .fold(String::new(), |mut acc, item| { - acc.push_str(&format!("{}\n", item)); - acc - }) - .trim_end_matches('\n') - ), + Err(err) => panic!("{:?}", err), } } fn graphql_query_derive_inner( input: proc_macro::TokenStream, -) -> Result { +) -> Result { let input = TokenStream::from(input); let ast = syn::parse2(input).context("Derive input parsing.")?; let (query_path, schema_path) = build_query_and_schema_path(&ast)?; @@ -37,13 +29,14 @@ fn graphql_query_derive_inner( Ok( generate_module_token_stream(query_path, &schema_path, options) .map(Into::into) + .map_err(|fail| fail.compat()) .context("Code generation failed.")?, ) } fn build_query_and_schema_path( input: &syn::DeriveInput, -) -> Result<(PathBuf, PathBuf), failure::Error> { +) -> Result<(PathBuf, PathBuf), anyhow::Error> { let cargo_manifest_dir = ::std::env::var("CARGO_MANIFEST_DIR") .context("Checking that the CARGO_MANIFEST_DIR env variable is defined.")?; @@ -60,7 +53,7 @@ fn build_query_and_schema_path( fn build_graphql_client_derive_options( input: &syn::DeriveInput, query_path: PathBuf, -) -> Result { +) -> Result { let variables_derives = attributes::extract_attr(input, "variables_derives").ok(); let response_derives = attributes::extract_attr(input, "response_derives").ok();