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§

The expected input type did not match the actual input type.

Examples found in repository?
src/types/external/string.rs (line 17)
14
15
16
17
18
19
    fn parse(value: Value) -> InputValueResult<Self> {
        match value {
            Value::String(s) => Ok(s),
            _ => Err(InputValueError::expected_type(value)),
        }
    }
More examples
Hide additional examples
src/types/external/bool.rs (line 9)
6
7
8
9
10
11
    fn parse(value: Value) -> InputValueResult<Self> {
        match value {
            Value::Boolean(n) => Ok(n),
            _ => Err(InputValueError::expected_type(value)),
        }
    }
src/types/external/bytes.rs (line 11)
8
9
10
11
12
13
    fn parse(value: Value) -> InputValueResult<Self> {
        match value {
            Value::Binary(data) => Ok(data),
            _ => Err(InputValueError::expected_type(value)),
        }
    }
src/types/external/chrono_tz.rs (line 14)
11
12
13
14
15
16
    fn parse(value: Value) -> InputValueResult<Self> {
        match value {
            Value::String(s) => Ok(s.parse()?),
            _ => Err(InputValueError::expected_type(value)),
        }
    }
src/types/external/smol_str.rs (line 14)
11
12
13
14
15
16
    fn parse(value: Value) -> InputValueResult<Self> {
        match value {
            Value::String(s) => Ok(SmolStr::new(s)),
            _ => Err(InputValueError::expected_type(value)),
        }
    }
src/types/external/url.rs (line 11)
8
9
10
11
12
13
    fn parse(value: Value) -> InputValueResult<Self> {
        match value {
            Value::String(s) => Ok(Url::parse(&s)?),
            _ => Err(InputValueError::expected_type(value)),
        }
    }

A custom error message.

Any type that implements Display is automatically converted to this if you use the ? operator.

Examples found in repository?
src/error.rs (line 245)
244
245
246
    fn from(error: E) -> Self {
        Self::custom(error)
    }
More examples
Hide additional examples
src/types/string_number.rs (line 30)
26
27
28
29
30
31
32
33
34
35
    fn parse(value: Value) -> InputValueResult<Self> {
        match value {
            Value::String(s) => {
                let n = T::from_str_radix(&s, 10)
                    .map_err(|err| InputValueError::custom(err.to_string()))?;
                Ok(StringNumber(n))
            }
            _ => Err(InputValueError::expected_type(value)),
        }
    }
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)),
        }
    }

Propagate the error message to a different type.

Set an extension value.

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
Hide additional 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))
    }

Trait Implementations§

Formats the value using the given formatter. Read more
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Attaches the current Context to this type, returning a WithContext wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more