This repository has been archived on 2023-11-08. You can view files and clone it, but cannot push or open issues or pull requests.
fiddle/src/api/error.rs

59 lines
1.7 KiB
Rust

use juniper::{graphql_value, FieldError, FieldResult, IntoFieldError, ScalarValue};
pub enum Error {
Internal,
DoesNotExist,
CountNegative,
}
impl<S: ScalarValue> IntoFieldError<S> for Error {
fn into_field_error(self) -> FieldError<S> {
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::CountNegative => FieldError::new(
"Count can not be negative",
graphql_value!({
"type": "COUNT_NEGATIVE",
}),
),
}
}
}
pub trait QueryResultIntoFieldResult<T> {
fn into_field_result(self) -> FieldResult<T>;
}
impl<T> QueryResultIntoFieldResult<T> for diesel::QueryResult<T> {
fn into_field_result(self) -> FieldResult<T> {
self.map_err(|_| Error::Internal.into_field_error())
}
}
// pub trait AsyncResultIntoFieldResult<T> {
// fn into_field_result(self) -> FieldResult<T>;
// }
// impl AsyncResultIntoFieldResult<celery::task::AsyncResult>
// for Result<celery::task::AsyncResult, celery::error::CeleryError>
// {
// fn into_field_result(self) -> FieldResult<celery::task::AsyncResult> {
// // match self {
// // Ok(x) => Ok(x),
// // Err(_) => Err(Error::Internal.into_field_error()),
// // }
// self.map_err(|_| Error::Internal.into_field_error())
// }
// }