Add report_error_with_extensions

This commit is contained in:
Rodgers 2021-07-08 20:02:49 +07:00
parent cb56008a81
commit c0b5c22c64
3 changed files with 36 additions and 11 deletions

View File

@ -58,9 +58,10 @@ impl<'a> Visitor<'a> for ArgumentsOfCorrectType<'a> {
if let Some(validator) = &arg.validator {
if let Some(value) = &value {
if let Err(reason) = validator.is_valid(value) {
ctx.report_error(
ctx.report_error_with_extensions(
vec![name.pos],
format!("Invalid value for argument \"{}\", {}", arg.name, reason),
None,
);
return;
}

View File

@ -31,10 +31,11 @@ impl<'a> CycleDetector<'a> {
*pos
};
self.errors.push(RuleError {
locations: vec![err_pos],
message: format!("Cannot spread fragment \"{}\"", name),
});
self.errors.push(RuleError::new(
vec![err_pos],
format!("Cannot spread fragment \"{}\"", name),
None,
));
} else if !self.visited.contains(name) {
path.push((name, *pos));
self.detect_from(name, path);

View File

@ -8,7 +8,9 @@ use crate::parser::types::{
OperationDefinition, OperationType, Selection, SelectionSet, TypeCondition, VariableDefinition,
};
use crate::registry::{self, MetaType, MetaTypeName};
use crate::{InputType, Name, Pos, Positioned, ServerError, ServerResult, Variables};
use crate::{
ErrorExtensionValues, InputType, Name, Pos, Positioned, ServerError, ServerResult, Variables,
};
#[doc(hidden)]
pub struct VisitorContext<'a> {
@ -37,10 +39,16 @@ impl<'a> VisitorContext<'a> {
}
pub(crate) fn report_error<T: Into<String>>(&mut self, locations: Vec<Pos>, msg: T) {
self.errors.push(RuleError {
locations,
message: msg.into(),
})
self.errors.push(RuleError::new(locations, msg, None));
}
pub(crate) fn report_error_with_extensions<T: Into<String>>(
&mut self,
locations: Vec<Pos>,
msg: T,
extensions: Option<ErrorExtensionValues>,
) {
self.errors.push(RuleError::new(locations, msg, extensions));
}
pub(crate) fn append_errors(&mut self, errors: Vec<RuleError>) {
@ -790,6 +798,21 @@ fn visit_inline_fragment<'a, V: Visitor<'a>>(
pub(crate) struct RuleError {
pub(crate) locations: Vec<Pos>,
pub(crate) message: String,
pub(crate) extensions: Option<ErrorExtensionValues>,
}
impl RuleError {
pub(crate) fn new(
locations: Vec<Pos>,
msg: impl Into<String>,
extensions: Option<ErrorExtensionValues>,
) -> Self {
Self {
locations,
message: msg.into(),
extensions,
}
}
}
impl Display for RuleError {
@ -819,7 +842,7 @@ impl From<RuleError> for ServerError {
message: e.message,
locations: e.locations,
path: Vec::new(),
extensions: None,
extensions: e.extensions,
}
}
}