gitea_pages/backend/src/api/error.rs

100 lines
3.0 KiB
Rust

use juniper::{graphql_value, FieldError, FieldResult, IntoFieldError, ScalarValue};
pub enum Error {
Internal,
DoesNotExist,
InvalidUuid,
InvalidCredentials,
Unauthenticated,
RepoAlreadyExists,
ExternalRepoDoesNotExist,
RepoPullBranchDoesNotExist,
}
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::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<T> {
fn into_field_result(self) -> FieldResult<T>;
}
impl<T> QueryResultIntoFieldResult<T> for diesel::QueryResult<T> {
fn into_field_result(self) -> FieldResult<T> {
// match self {
// Ok(x) => Ok(x),
// Err(_) => Err(Error::Internal.into_field_error()),
// }
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())
}
}