async-graphql/src/error.rs

107 lines
2.8 KiB
Rust
Raw Normal View History

2020-03-01 10:54:34 +00:00
use crate::Error;
use graphql_parser::query::Value;
use graphql_parser::Pos;
use std::fmt::{Debug, Display, Formatter};
#[derive(Debug, Error)]
#[error("{0}")]
2020-03-01 13:35:39 +00:00
pub struct QueryParseError(pub(crate) String);
2020-03-01 10:54:34 +00:00
#[derive(Debug, Error)]
2020-03-01 13:35:39 +00:00
pub enum QueryError {
2020-03-01 10:54:34 +00:00
#[error("Not supported.")]
NotSupported,
#[error("Expected type \"{expect}\", found {actual}.")]
2020-03-03 11:15:18 +00:00
ExpectedType { expect: String, actual: Value },
2020-03-01 10:54:34 +00:00
2020-03-01 16:52:05 +00:00
#[error("Expected type \"{expect}\", found {actual}.")]
ExpectedJsonType {
2020-03-03 11:15:18 +00:00
expect: String,
2020-03-01 16:52:05 +00:00
actual: serde_json::Value,
},
2020-03-01 10:54:34 +00:00
#[error("Cannot query field \"{field_name}\" on type \"{object}\".")]
2020-03-06 15:58:43 +00:00
FieldNotFound { field_name: String, object: String },
2020-03-01 10:54:34 +00:00
#[error("Unknown operation named \"{name}\"")]
UnknownOperationNamed { name: String },
#[error("Type \"{object}\" must have a selection of subfields.")]
2020-03-06 15:58:43 +00:00
MustHaveSubFields { object: String },
2020-03-01 10:54:34 +00:00
#[error("Schema is not configured for mutations.")]
NotConfiguredMutations,
2020-03-02 00:24:49 +00:00
#[error("Invalid value for enum \"{ty}\".")]
2020-03-03 11:15:18 +00:00
InvalidEnumValue { ty: String, value: String },
2020-03-01 13:35:39 +00:00
#[error("Required field \"{field_name}\" for InputObject \"{object}\" does not exist.")]
RequiredField {
field_name: String,
object: &'static str,
},
#[error("Variable \"${var_name}\" is not defined")]
VarNotDefined { var_name: String },
#[error(
"Directive \"{directive}\" argument \"{arg_name}\" of type \"{arg_type}\" is required, but it was not provided."
)]
RequiredDirectiveArgs {
directive: &'static str,
arg_name: &'static str,
arg_type: &'static str,
},
#[error("Unknown directive \"{name}\".")]
UnknownDirective { name: String },
2020-03-05 07:50:57 +00:00
#[error("Unknown fragment \"{name}\".")]
UnknownFragment { name: String },
2020-03-07 02:39:55 +00:00
#[error("Object \"{object}\" does not implement interface \"{interface}\"")]
NotImplementedInterface { object: String, interface: String },
#[error("Unrecognized inline fragment \"{name}\" on type \"{object}\"")]
UnrecognizedInlineFragment { object: String, name: String },
2020-03-01 10:54:34 +00:00
}
2020-03-01 13:35:39 +00:00
pub trait ErrorWithPosition {
2020-03-01 10:54:34 +00:00
type Result;
2020-03-01 13:35:39 +00:00
fn with_position(self, position: Pos) -> PositionError;
2020-03-01 10:54:34 +00:00
}
2020-03-01 13:35:39 +00:00
impl<T: Into<Error>> ErrorWithPosition for T {
type Result = PositionError;
2020-03-01 10:54:34 +00:00
2020-03-01 13:35:39 +00:00
fn with_position(self, position: Pos) -> PositionError {
PositionError {
2020-03-01 10:54:34 +00:00
position,
inner: self.into(),
}
}
}
#[derive(Debug, Error)]
2020-03-01 13:35:39 +00:00
pub struct PositionError {
2020-03-01 10:54:34 +00:00
pub position: Pos,
pub inner: Error,
}
2020-03-01 13:35:39 +00:00
impl PositionError {
2020-03-01 10:54:34 +00:00
pub fn new(position: Pos, inner: Error) -> Self {
Self { position, inner }
}
pub fn into_inner(self) -> Error {
self.inner
}
}
2020-03-01 13:35:39 +00:00
impl Display for PositionError {
2020-03-01 10:54:34 +00:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
2020-03-05 00:39:56 +00:00
write!(f, "{}", self.inner)
2020-03-01 10:54:34 +00:00
}
}