use juniper::{graphql_value, FieldError, FieldResult, IntoFieldError, ScalarValue}; pub enum Error { Internal, DoesNotExist, InvalidUuid, InvalidCredentials, Unauthenticated, RepoAlreadyExists, ExternalRepoDoesNotExist, RepoPullBranchDoesNotExist, } impl IntoFieldError for Error { fn into_field_error(self) -> FieldError { match self { Self::Internal => FieldError::new( "Internal server error", graphql_value!({ "type": "INTERNAL" }), ), Self::DoesNotExist => FieldError::new( "Record does not exist", graphql_value!({ "type": "DOES_NOT_EXIST" }), ), Self::InvalidUuid => FieldError::new( "Invalid UUID", graphql_value!({ "type": "INVALID_UUID", }), ), Self::InvalidCredentials => FieldError::new( "Invalid credentials", graphql_value!({ "type": "INVALID_CREDENTIALS", }), ), Self::Unauthenticated => FieldError::new( "Unauthenticated", graphql_value!({ "type": "UNAUTHENTICATED", }), ), Self::RepoAlreadyExists => FieldError::new( "Repository already exists", graphql_value!({ "type": "REPO_ALREADY_EXISTS", }), ), Self::ExternalRepoDoesNotExist => FieldError::new( "Repository does not exist on Git server", graphql_value!({ "type": "EXTERNAL_REPO_DOES_NOT_EXIST", }), ), Self::RepoPullBranchDoesNotExist => FieldError::new( "Repository does not have pages branch", graphql_value!({ "type": "REPO_PULL_BRANCH_DOES_NOT_EXIST", }), ), } } } pub trait QueryResultIntoFieldResult { fn into_field_result(self) -> FieldResult; } impl QueryResultIntoFieldResult for diesel::QueryResult { fn into_field_result(self) -> FieldResult { // match self { // Ok(x) => Ok(x), // Err(_) => Err(Error::Internal.into_field_error()), // } self.map_err(|_| Error::Internal.into_field_error()) } } pub trait AsyncResultIntoFieldResult { fn into_field_result(self) -> FieldResult; } impl AsyncResultIntoFieldResult for Result { fn into_field_result(self) -> FieldResult { // match self { // Ok(x) => Ok(x), // Err(_) => Err(Error::Internal.into_field_error()), // } self.map_err(|_| Error::Internal.into_field_error()) } }