Skip to content

Commit dd424f3

Browse files
andy128kLegNeato
authored andcommitted
Bubble up scalar error (#434)
1 parent 61c0543 commit dd424f3

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

juniper/src/parser/parser.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub enum ParseError<'a> {
1313

1414
/// An error during tokenization occurred
1515
LexerError(LexerError),
16+
17+
/// A scalar of unexpected type occurred in the source
18+
ExpectedScalarError(&'static str),
1619
}
1720

1821
#[doc(hidden)]
@@ -196,6 +199,7 @@ impl<'a> fmt::Display for ParseError<'a> {
196199
ParseError::UnexpectedToken(ref token) => write!(f, "Unexpected \"{}\"", token),
197200
ParseError::UnexpectedEndOfFile => write!(f, "Unexpected end of input"),
198201
ParseError::LexerError(ref err) => err.fmt(f),
202+
ParseError::ExpectedScalarError(err) => err.fmt(f),
199203
}
200204
}
201205
}

juniper/src/parser/tests/document.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
},
55
parser::{document::parse_document_source, ParseError, SourcePosition, Spanning, Token},
66
schema::model::SchemaType,
7+
types::scalars::EmptyMutation,
78
validation::test_harness::{MutationRoot, QueryRoot},
89
value::{DefaultScalarValue, ScalarRefValue, ScalarValue},
910
};
@@ -145,3 +146,23 @@ fn errors() {
145146
)
146147
);
147148
}
149+
150+
#[test]
151+
fn issue_427_panic_is_not_expected() {
152+
struct QueryWithoutFloat;
153+
154+
#[crate::object_internal]
155+
impl QueryWithoutFloat {
156+
fn echo(value: String) -> String {
157+
value
158+
}
159+
}
160+
161+
let schema = SchemaType::new::<QueryWithoutFloat, EmptyMutation<()>>(&(), &());
162+
let parse_result = parse_document_source(r##"{ echo(value: 123.0) }"##, &schema);
163+
164+
assert_eq!(
165+
parse_result.unwrap_err().item,
166+
ParseError::ExpectedScalarError("There needs to be a Float type")
167+
);
168+
}

juniper/src/parser/value.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,33 +210,36 @@ fn parse_scalar_literal_by_infered_type<'a, 'b, S>(
210210
where
211211
S: ScalarValue,
212212
{
213-
match token {
213+
let result = match token {
214214
ScalarToken::String(_) => {
215215
if let Some(&MetaType::Scalar(ref s)) = schema.concrete_type_by_name("String") {
216-
(s.parse_fn)(token)
217-
.map(|s| Spanning::start_end(start, end, InputValue::Scalar(s)))
218-
.map_err(|e| Spanning::start_end(start, end, e))
216+
(s.parse_fn)(token).map(InputValue::Scalar)
219217
} else {
220-
panic!("There needs to be a String type")
218+
Err(ParseError::ExpectedScalarError(
219+
"There needs to be a String type",
220+
))
221221
}
222222
}
223223
ScalarToken::Int(_) => {
224224
if let Some(&MetaType::Scalar(ref s)) = schema.concrete_type_by_name("Int") {
225-
(s.parse_fn)(token)
226-
.map(|s| Spanning::start_end(start, end, InputValue::Scalar(s)))
227-
.map_err(|e| Spanning::start_end(start, end, e))
225+
(s.parse_fn)(token).map(InputValue::Scalar)
228226
} else {
229-
panic!("There needs to be a Int type")
227+
Err(ParseError::ExpectedScalarError(
228+
"There needs to be an Int type",
229+
))
230230
}
231231
}
232232
ScalarToken::Float(_) => {
233233
if let Some(&MetaType::Scalar(ref s)) = schema.concrete_type_by_name("Float") {
234-
(s.parse_fn)(token)
235-
.map(|s| Spanning::start_end(start, end, InputValue::Scalar(s)))
236-
.map_err(|e| Spanning::start_end(start, end, e))
234+
(s.parse_fn)(token).map(InputValue::Scalar)
237235
} else {
238-
panic!("There needs to be a Float type")
236+
Err(ParseError::ExpectedScalarError(
237+
"There needs to be a Float type",
238+
))
239239
}
240240
}
241-
}
241+
};
242+
result
243+
.map(|s| Spanning::start_end(start, end, s))
244+
.map_err(|e| Spanning::start_end(start, end, e))
242245
}

0 commit comments

Comments
 (0)