graphql_id/
error.rs

1use graphql_parser::query::*;
2use std::fmt;
3
4/// # GraphQLError
5#[derive(Debug)]
6pub enum GraphQLError {
7    /// The target operation is not found in the Query
8    OperationNotFound,
9
10    /// A fragment used in the target operation is not found
11    FragmentNotFound,
12
13    /// A fragment contains itself or contains other fragments that cause a loop
14    InfiniteFragmentRecursionError(String),
15
16    /// GraphQL Parse error
17    Parse(ParseError),
18
19    /// A default operation cannot be chosen as the input contains multiple operations
20    MultipleOperation,
21
22    /// An operation always requires an operation name (not a limitation in GraphQL. Just a limitation in this library)
23    AnonymousOperation,
24}
25
26impl fmt::Display for GraphQLError {
27    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28        match *self {
29            GraphQLError::Parse(ref e) => e.fmt(f),
30            GraphQLError::OperationNotFound => {
31                write!(f, "The input operation is not found in the document")
32            }
33            GraphQLError::FragmentNotFound => {
34                write!(f, "The query does not contain the fragment which is used")
35            }
36            GraphQLError::InfiniteFragmentRecursionError(ref fragment_name) => {
37                write!(f, "Infinte Fragment Recursion detected {}", fragment_name)
38            }
39            GraphQLError::MultipleOperation => write!(
40                f,
41                "Multiple Operations found in the query. Operation Name is required"
42            ),
43            GraphQLError::AnonymousOperation => write!(
44                f,
45                "Cannot generate id for anonymous query. Use an operation name"
46            ),
47        }
48    }
49}
50
51impl From<ParseError> for GraphQLError {
52    fn from(err: ParseError) -> GraphQLError {
53        GraphQLError::Parse(err)
54    }
55}