Struct async_graphql::InputValueError
source · pub struct InputValueError<T> { /* private fields */ }
Expand description
An error parsing an input value.
This type is generic over T as it uses T’s type name when converting to a regular error.
Implementations§
source§impl<T: InputType> InputValueError<T>
impl<T: InputType> InputValueError<T>
sourcepub fn expected_type(actual: Value) -> Self
pub fn expected_type(actual: Value) -> Self
The expected input type did not match the actual input type.
Examples found in repository?
More examples
Additional examples can be found in:
- src/types/external/uuid.rs
- src/types/external/uuid08.rs
- src/types/external/bson.rs
- src/types/external/time_offset_date_time.rs
- src/types/external/time_date.rs
- src/types/external/datetime.rs
- src/types/external/naive_time.rs
- src/types/external/time_primitive_date_time.rs
- src/types/external/duration.rs
- src/types/id.rs
- src/types/connection/edge.rs
- src/types/external/floats.rs
- src/types/string_number.rs
- src/types/upload.rs
- src/resolver_utils/enum.rs
- src/types/external/char.rs
- src/types/external/decimal.rs
- src/types/external/big_decimal.rs
- src/types/external/integers.rs
- src/types/external/non_zero_integers.rs
- src/types/external/json_object/btreemap.rs
- src/types/external/json_object/hashbrown_hashmap.rs
- src/types/external/json_object/hashmap.rs
- src/types/external/list/array.rs
sourcepub fn custom(msg: impl Display) -> Self
pub fn custom(msg: impl Display) -> Self
A custom error message.
Any type that implements Display
is automatically converted to this if
you use the ?
operator.
Examples found in repository?
More examples
src/resolver_utils/enum.rs (lines 32-35)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
pub fn parse_enum<T: EnumType + InputType>(value: Value) -> InputValueResult<T> {
let value = match &value {
Value::Enum(s) => s,
Value::String(s) => s.as_str(),
_ => return Err(InputValueError::expected_type(value)),
};
T::items()
.iter()
.find(|item| item.name == value)
.map(|item| item.value)
.ok_or_else(|| {
InputValueError::custom(format_args!(
r#"Enumeration type does not contain value "{}"."#,
value,
))
})
}
src/types/external/char.rs (lines 14-16)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::String(s) => {
let mut chars = s.chars();
match chars.next() {
Some(ch) if chars.next().is_none() => Ok(ch),
Some(_) => Err(InputValueError::custom(
"There can only be one unicode character in the string.",
)),
None => Err(InputValueError::custom("A unicode character is required.")),
}
}
_ => Err(InputValueError::expected_type(value)),
}
}
src/types/external/json_object/btreemap.rs (line 46)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn parse(value: Option<Value>) -> InputValueResult<Self> {
let value = value.unwrap_or_default();
match value {
Value::Object(map) => map
.into_iter()
.map(|(name, value)| {
Ok((
K::from_str(&name).map_err(|err| {
InputValueError::<Self>::custom(format!("object key: {}", err))
})?,
from_value(value).map_err(|err| format!("object value: {}", err))?,
))
})
.collect::<Result<_, _>>()
.map_err(InputValueError::propagate),
_ => Err(InputValueError::expected_type(value)),
}
}
src/types/external/json_object/hashbrown_hashmap.rs (line 40)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn parse(value: Option<Value>) -> InputValueResult<Self> {
let value = value.unwrap_or_default();
match value {
Value::Object(map) => map
.into_iter()
.map(|(name, value)| {
Ok((
K::from_str(&name).map_err(|err| {
InputValueError::<Self>::custom(format!("object key: {}", err))
})?,
from_value(value).map_err(|err| format!("object value: {}", err))?,
))
})
.collect::<Result<_, _>>()
.map_err(InputValueError::propagate),
_ => Err(InputValueError::expected_type(value)),
}
}
Additional examples can be found in:
sourcepub fn propagate<U: InputType>(self) -> InputValueError<U>
pub fn propagate<U: InputType>(self) -> InputValueError<U>
Propagate the error message to a different type.
sourcepub fn with_extension(
self,
name: impl AsRef<str>,
value: impl Into<Value>
) -> Self
pub fn with_extension(
self,
name: impl AsRef<str>,
value: impl Into<Value>
) -> Self
Set an extension value.
sourcepub fn into_server_error(self, pos: Pos) -> ServerError
pub fn into_server_error(self, pos: Pos) -> ServerError
Convert the error into a server error.
Examples found in repository?
src/context.rs (line 600)
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
fn get_param_value<Q: InputType>(
&self,
arguments: &[(Positioned<Name>, Positioned<InputValue>)],
name: &str,
default: Option<fn() -> Q>,
) -> ServerResult<(Pos, Q)> {
let value = arguments
.iter()
.find(|(n, _)| n.node.as_str() == name)
.map(|(_, value)| value)
.cloned();
if value.is_none() {
if let Some(default) = default {
return Ok((Pos::default(), default()));
}
}
let (pos, value) = match value {
Some(value) => (value.pos, Some(self.resolve_input_value(value)?)),
None => (Pos::default(), None),
};
InputType::parse(value)
.map(|value| (pos, value))
.map_err(|e| e.into_server_error(pos))
}
#[doc(hidden)]
#[must_use]
pub fn with_index(&'a self, idx: usize) -> ContextBase<'a, T>
where
T: Copy,
{
ContextBase {
path_node: Some(QueryPathNode {
parent: self.path_node.as_ref(),
segment: QueryPathSegment::Index(idx),
}),
is_for_introspection: self.is_for_introspection,
item: self.item,
schema_env: self.schema_env,
query_env: self.query_env,
}
}
}
impl<'a> ContextBase<'a, &'a Positioned<Field>> {
#[doc(hidden)]
pub fn param_value<T: InputType>(
&self,
name: &str,
default: Option<fn() -> T>,
) -> ServerResult<(Pos, T)> {
self.get_param_value(&self.item.node.arguments, name, default)
}
#[doc(hidden)]
pub fn oneof_param_value<T: OneofObjectType>(&self) -> ServerResult<(Pos, T)> {
use indexmap::IndexMap;
let mut map = IndexMap::new();
for (name, value) in &self.item.node.arguments {
let value = self.resolve_input_value(value.clone())?;
map.insert(name.node.clone(), value);
}
InputType::parse(Some(Value::Object(map)))
.map(|value| (self.item.pos, value))
.map_err(|e| e.into_server_error(self.item.pos))
}
More examples
src/validation/visitor.rs (line 142)
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
pub fn param_value<T: InputType>(
&self,
variable_definitions: &[Positioned<VariableDefinition>],
field: &Field,
name: &str,
default: Option<fn() -> T>,
) -> ServerResult<T> {
let value = field.get_argument(name).cloned();
if value.is_none() {
if let Some(default) = default {
return Ok(default());
}
}
let (pos, value) = match value {
Some(value) => {
let pos = value.pos;
(
pos,
Some(value.node.into_const_with(|name| {
variable_definitions
.iter()
.find(|def| def.node.name.node == name)
.and_then(|def| {
if let Some(variables) = self.variables {
variables
.get(&def.node.name.node)
.or_else(|| def.node.default_value())
} else {
None
}
})
.cloned()
.ok_or_else(|| {
ServerError::new(
format!("Variable {} is not defined.", name),
Some(pos),
)
})
})?),
)
}
None => (Pos::default(), None),
};
T::parse(value).map_err(|e| e.into_server_error(pos))
}