Bubble up scalar error (#434)
This commit is contained in:
parent
61c0543523
commit
dd424f3579
3 changed files with 42 additions and 14 deletions
|
@ -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)]
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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},
|
||||
};
|
||||
|
@ -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")
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue