Skip to content

Bubble up scalar error #434

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 2 commits into from
Oct 24, 2019
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
4 changes: 4 additions & 0 deletions juniper/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub enum ParseError<'a> {

/// An error during tokenization occurred
LexerError(LexerError),

/// A scalar of unexpected type occurred in the source
ExpectedScalarError(&'static str),
}

#[doc(hidden)]
Expand Down Expand Up @@ -196,6 +199,7 @@ impl<'a> fmt::Display for ParseError<'a> {
ParseError::UnexpectedToken(ref token) => write!(f, "Unexpected \"{}\"", token),
ParseError::UnexpectedEndOfFile => write!(f, "Unexpected end of input"),
ParseError::LexerError(ref err) => err.fmt(f),
ParseError::ExpectedScalarError(err) => err.fmt(f),
}
}
}
21 changes: 21 additions & 0 deletions juniper/src/parser/tests/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
},
parser::{document::parse_document_source, ParseError, SourcePosition, Spanning, Token},
schema::model::SchemaType,
types::scalars::EmptyMutation,
validation::test_harness::{MutationRoot, QueryRoot},
value::{DefaultScalarValue, ScalarRefValue, ScalarValue},
};
Expand Down Expand Up @@ -145,3 +146,23 @@ fn errors() {
)
);
}

#[test]
fn issue_427_panic_is_not_expected() {
struct QueryWithoutFloat;

#[crate::object_internal]
impl QueryWithoutFloat {
fn echo(value: String) -> String {
value
}
}

let schema = SchemaType::new::<QueryWithoutFloat, EmptyMutation<()>>(&(), &());
let parse_result = parse_document_source(r##"{ echo(value: 123.0) }"##, &schema);

assert_eq!(
parse_result.unwrap_err().item,
ParseError::ExpectedScalarError("There needs to be a Float type")
);
}
31 changes: 17 additions & 14 deletions juniper/src/parser/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,33 +210,36 @@ fn parse_scalar_literal_by_infered_type<'a, 'b, S>(
where
S: ScalarValue,
{
match token {
let result = match token {
ScalarToken::String(_) => {
if let Some(&MetaType::Scalar(ref s)) = schema.concrete_type_by_name("String") {
(s.parse_fn)(token)
.map(|s| Spanning::start_end(start, end, InputValue::Scalar(s)))
.map_err(|e| Spanning::start_end(start, end, e))
(s.parse_fn)(token).map(InputValue::Scalar)
} else {
panic!("There needs to be a String type")
Err(ParseError::ExpectedScalarError(
"There needs to be a String type",
))
}
}
ScalarToken::Int(_) => {
if let Some(&MetaType::Scalar(ref s)) = schema.concrete_type_by_name("Int") {
(s.parse_fn)(token)
.map(|s| Spanning::start_end(start, end, InputValue::Scalar(s)))
.map_err(|e| Spanning::start_end(start, end, e))
(s.parse_fn)(token).map(InputValue::Scalar)
} else {
panic!("There needs to be a Int type")
Err(ParseError::ExpectedScalarError(
"There needs to be an Int type",
))
}
}
ScalarToken::Float(_) => {
if let Some(&MetaType::Scalar(ref s)) = schema.concrete_type_by_name("Float") {
(s.parse_fn)(token)
.map(|s| Spanning::start_end(start, end, InputValue::Scalar(s)))
.map_err(|e| Spanning::start_end(start, end, e))
(s.parse_fn)(token).map(InputValue::Scalar)
} else {
panic!("There needs to be a Float type")
Err(ParseError::ExpectedScalarError(
"There needs to be a Float type",
))
}
}
}
};
result
.map(|s| Spanning::start_end(start, end, s))
.map_err(|e| Spanning::start_end(start, end, e))
}