Skip to content

Commit

Permalink
Bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
tyranron committed Nov 15, 2023
1 parent 2215cd0 commit 439bda6
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ jobs:
matrix:
include:
- { feature: <none>, crate: juniper }
- { feature: anyhow, crate: juniper }
- { feature: anyhow,backtrace, crate: juniper }
- { feature: bigdecimal, crate: juniper }
- { feature: bson, crate: juniper }
- { feature: chrono, crate: juniper }
Expand Down
5 changes: 4 additions & 1 deletion juniper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ default = [
"url",
"uuid",
]
anyhow = ["dep:anyhow"]
backtrace = ["anyhow?/backtrace"]
bigdecimal = ["dep:bigdecimal", "dep:num-bigint", "dep:ryu"]
bson = ["dep:bson"]
chrono = ["dep:chrono"]
Expand All @@ -46,7 +48,7 @@ url = ["dep:url"]
uuid = ["dep:uuid"]

[dependencies]
anyhow = { version = "1.0.47", default-features = false, optional = true }
anyhow = { version = "1.0.47", optional = true }
async-trait = "0.1.39"
bigdecimal = { version = "0.4", optional = true }
bson = { version = "2.4", features = ["chrono-0_4"], optional = true }
Expand Down Expand Up @@ -81,6 +83,7 @@ bencher = "0.1.2"
chrono = { version = "0.4.30", features = ["alloc"], default-features = false }
pretty_assertions = "1.0.0"
serde_json = "1.0.18"
serial_test = "2.0"
tokio = { version = "1.0", features = ["macros", "time", "rt-multi-thread"] }

[[bench]]
Expand Down
102 changes: 102 additions & 0 deletions juniper/src/integrations/anyhow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//! GraphQL support for [`anyhow::Error`].
use crate::{FieldError, IntoFieldError, ScalarValue, Value};

impl<S: ScalarValue> IntoFieldError<S> for anyhow::Error {
fn into_field_error(self) -> FieldError<S> {
#[cfg(any(nightly, feature = "backtrace"))]
let extensions = {
let backtrace = self.backtrace().to_string();
if backtrace == "disabled backtrace" {
Value::Null
} else {
let mut obj = crate::value::Object::with_capacity(1);
_ = obj.add_field(
"backtrace",
Value::List(
backtrace
.split('\n')
.map(|line| Value::Scalar(line.to_owned().into()))
.collect(),
),
);
Value::Object(obj)
}
};
#[cfg(not(any(nightly, feature = "backtrace")))]
let extensions = Value::Null;

FieldError::new(self, extensions)
}
}

#[cfg(test)]
mod test {
use std::env;

use anyhow::anyhow;
use serial_test::serial;

use crate::{
execute, graphql_object, graphql_value, graphql_vars, parser::SourcePosition,
EmptyMutation, EmptySubscription, RootNode,
};

#[tokio::test]
#[serial]
async fn simple() {
struct Root;

#[graphql_object]
impl Root {
fn err() -> anyhow::Result<i32> {
Err(anyhow!("errored!"))
}
}

let prev_env = env::var("RUST_BACKTRACE").ok();
env::set_var("RUST_BACKTRACE", "1");

const DOC: &str = r#"{
err
}"#;

let schema = RootNode::new(
Root,
EmptyMutation::<()>::new(),
EmptySubscription::<()>::new(),
);

let res = execute(DOC, None, &schema, &graphql_vars! {}, &()).await;

assert!(res.is_ok(), "failed: {:?}", res.unwrap_err());

let (val, errs) = res.unwrap();

assert_eq!(val, graphql_value!(null));
assert_eq!(errs.len(), 1, "too many errors: {errs:?}");

let err = errs.first().unwrap();

assert_eq!(*err.location(), SourcePosition::new(14, 1, 12));
assert_eq!(err.path(), &["err"]);

let err = err.error();

assert_eq!(err.message(), "errored!");
#[cfg(not(any(nightly, feature = "backtrace")))]
assert_eq!(err.extensions(), &graphql_value!(null));
#[cfg(any(nightly, feature = "backtrace"))]
assert_eq!(
err.extensions()
.as_object_value()
.map(|ext| ext.contains_field("backtrace")),
Some(true),
"no `backtrace` in extensions: {err:?}",
);

if let Some(val) = prev_env {
env::set_var("RUST_BACKTRACE", val);
}
}
}
2 changes: 2 additions & 0 deletions juniper/src/integrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Provides GraphQLType implementations for some external types
#[cfg(feature = "anyhow")]
pub mod anyhow;
#[cfg(feature = "bigdecimal")]
pub mod bigdecimal;
#[cfg(feature = "bson")]
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/tests/codegen_object_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ mod fallible_method {

impl<S: ScalarValue> IntoFieldError<S> for CustomError {
fn into_field_error(self) -> FieldError<S> {
juniper::FieldError::new("Whatever", graphql_value!({"code": "some"}))
FieldError::new("Whatever", graphql_value!({"code": "some"}))
}
}

Expand Down

0 comments on commit 439bda6

Please sign in to comment.