thegraph/common: Add GraphQLServerError::JSONError variant
We will use this to report JSON parsing errors.
use futures::prelude::*;
use futures::sync::oneshot::Canceled;
use futures::sync::mpsc::{Receiver, Sender};
+use serde_json;
use std::error::Error;
use std::fmt;
use super::query::{Query, QueryError};
@@ -12,6 +13,7 @@ use super::util::stream::StreamError;
#[derive(Debug)]
pub enum GraphQLServerError {
Canceled(Canceled),
+ JSONError(serde_json::Error),
QueryError(QueryError),
InternalError(&'static str),
}
@@ -22,6 +24,12 @@ impl From<Canceled> for GraphQLServerError {
}
}
+impl From<serde_json::Error> for GraphQLServerError {
+ fn from(e: serde_json::Error) -> Self {
+ GraphQLServerError::JSONError(e)
+ }
+}
+
impl From<QueryError> for GraphQLServerError {
fn from(e: QueryError) -> Self {
GraphQLServerError::QueryError(e)
@@ -38,6 +46,7 @@ impl fmt::Display for GraphQLServerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&GraphQLServerError::Canceled(ref e) => write!(f, "Query was canceled: {}", e),
+ &GraphQLServerError::JSONError(ref e) => write!(f, "JSON error: {}", e),
&GraphQLServerError::QueryError(ref e) => write!(f, "Query error: {}", e),
&GraphQLServerError::InternalError(ref s) => write!(f, "Internal error: {}", s),
}
@@ -52,6 +61,7 @@ impl Error for GraphQLServerError {
fn cause(&self) -> Option<&Error> {
match self {
&GraphQLServerError::Canceled(ref e) => Some(e),
+ &GraphQLServerError::JSONError(ref e) => Some(e),
&GraphQLServerError::QueryError(ref e) => Some(e),
&GraphQLServerError::InternalError(_) => None,
}
@@ -59,7 +69,7 @@ impl Error for GraphQLServerError {
}
/// Common trait for GraphQL server implementations.
-pub trait GraphQLServer<T> {
+pub trait GraphQLServer {
/// Sender to which others should write whenever the schema that the server
/// should serve changes.
fn schema_provider_event_sink(&mut self) -> Sender<SchemaProviderEvent>;
@@ -70,7 +80,7 @@ pub trait GraphQLServer<T> {
/// Receiver from which others can read incoming queries for processing.
/// Can only be called once. Any consecutive call will result in a StreamError.
- fn query_stream(&mut self) -> Result<Receiver<Query<T>>, StreamError>;
+ fn query_stream(&mut self) -> Result<Receiver<Query>, StreamError>;
/// Creates a new Tokio task that, when spawned, brings up the GraphQL server.
fn serve(&mut self) -> Result<Box<Future<Item = (), Error = ()> + Send>, GraphQLServerError>;
