use cynic::http::CynicReqwestError;
use std::{io, path::PathBuf};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ApiError {
#[error("could not start the login server")]
StartLoginServer,
#[error("could not proceed as you are not logged in")]
NotLoggedIn,
#[error("could not delete '~/.grafbase/credentials.json'\ncaused by: {0}")]
DeleteCredentialsFile(io::Error),
#[error("could not delete '~/.grafbase/project.json'\ncaused by: {0}")]
DeleteProjectMetadataFile(io::Error),
#[error("could not read '~/.grafbase/credentials.json'\ncaused by: {0}")]
ReadCredentialsFile(io::Error),
#[error("could not read '.grafbase/project.json'\ncaused by: {0}")]
ReadProjectMetadataFile(io::Error),
#[error("could not read '~/.grafbase'\ncaused by: {0}")]
ReadUserDotGrafbaseFolder(io::Error),
#[error("could not read '.grafbase'\ncaused by: {0}")]
ReadProjectDotGrafbaseFolder(io::Error),
#[error("could not complete the action as this project has not been linked")]
UnlinkedProject,
#[error("could not complete the action as your credential file is corrupt")]
CorruptCredentialsFile,
#[error("could not complete the action as your access token is corrupt")]
CorruptAccessToken,
#[error("could not complete the action as your project metadata file are corrupt")]
CorruptProjectMetadataFile,
#[error("unauthorized or deleted user")]
UnauthorizedOrDeletedUser,
#[error("incorrectly scoped token")]
IncorrectlyScopedToken,
#[error("could not read the project graphql schema")]
ReadSchema,
#[error("could not write the project metadata file\ncaused by: {0}")]
WriteProjectMetadataFile(io::Error),
#[error("could not complete a request")]
RequestError,
#[error("could not complete a request")]
ConnectionError,
#[error("could not proceed as this local project has already been linked to a remote project")]
ProjectAlreadyLinked,
#[error("could not find the current user home folder")]
FindUserDotGrafbaseFolder,
#[error("could not create '~/.grafbase'\ncaused by: {0}")]
CreateUserDotGrafbaseFolder(io::Error),
#[error("could not create '.grafbase'\ncaused by: {0}")]
CreateProjectDotGrafbaseFolder(io::Error),
#[error("could not find an available port")]
FindAvailablePort,
#[error("could not complete the request to upload the deployment archive")]
UploadError,
#[error("could not read the upload archive metadata\ncaused by: {0}")]
ReadArchiveMetadata(io::Error),
#[error("could not read the upload archive\ncaused by: {0}")]
ReadArchive(io::Error),
#[error("could not append a file or directory to the upload archive\ncaused by: {0}")]
AppendToArchive(io::Error),
#[error("could not create a temporary file\ncaused by: {0}")]
CreateTempFile(io::Error),
#[error(transparent)]
CreateError(#[from] CreateError),
#[error(transparent)]
DeployError(#[from] DeployError),
}
#[derive(Error, Debug)]
pub enum CreateError {
#[error("could not create a new project as the provided slug is already in use")]
SlugAlreadyExists,
#[error("could not create a new project as the provided slug is invalid")]
SlugInvalid,
#[error("could not create a new project as the provided slug is longer than {max_length} characters")]
SlugTooLong { max_length: i32 },
#[error("could not create a new project as the specified account ID does not exist")]
AccountDoesNotExist,
#[error("could not create a new project as the current plan limit of {max} projects has been reached")]
CurrentPlanLimitReached { max: i32 },
#[error("could not create a new project as duplicate database regions were selected")]
DuplicateDatabaseRegions { duplicates: Vec<String> },
#[error("could not create a new project as no database regions were selected")]
EmptyDatabaseRegions,
#[error("could not create a new project as invalid regions were selected")]
InvalidDatabaseRegions { invalid: Vec<String> },
#[error("could not create a new project, encountered an unknown error")]
Unknown,
}
#[derive(Error, Debug)]
pub enum DeployError {
#[error("could not deploy as the linked project does not exist")]
ProjectDoesNotExist,
#[error("could not deploy as the created archive size is above the allowed limit of {limit} bytes")]
ArchiveFileSizeLimitExceeded { limit: i32 },
#[error("could not deploy as you have reached the allowed daily deployemnt amount of {limit}")]
DailyDeploymentCountLimitExceeded { limit: i32 },
#[error("could not deploy, encountered an unknown error")]
Unknown,
}
#[derive(Error, Debug)]
pub enum LoginApiError {
#[error("could not write '{0}'")]
WriteCredentialFile(PathBuf),
}
impl From<CynicReqwestError> for ApiError {
fn from(error: CynicReqwestError) -> Self {
match error {
CynicReqwestError::ReqwestError(error) if error.is_connect() => ApiError::ConnectionError,
CynicReqwestError::ReqwestError(_) | CynicReqwestError::ErrorResponse(_, _) => ApiError::RequestError,
}
}
}