grafbase_local_backend/api/
errors.rs1use cynic::http::CynicReqwestError;
2use std::{io, path::PathBuf};
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum ApiError {
7 #[error("could not start the login server")]
9 StartLoginServer,
10
11 #[error("could not proceed as you are not logged in")]
13 NotLoggedIn,
14
15 #[error("could not delete '~/.grafbase/credentials.json'\ncaused by: {0}")]
17 DeleteCredentialsFile(io::Error),
18
19 #[error("could not delete '~/.grafbase/project.json'\ncaused by: {0}")]
21 DeleteProjectMetadataFile(io::Error),
22
23 #[error("could not read '~/.grafbase/credentials.json'\ncaused by: {0}")]
25 ReadCredentialsFile(io::Error),
26
27 #[error("could not read '.grafbase/project.json'\ncaused by: {0}")]
29 ReadProjectMetadataFile(io::Error),
30
31 #[error("could not read '~/.grafbase'\ncaused by: {0}")]
33 ReadUserDotGrafbaseFolder(io::Error),
34
35 #[error("could not read '.grafbase'\ncaused by: {0}")]
37 ReadProjectDotGrafbaseFolder(io::Error),
38
39 #[error("could not complete the action as this project has not been linked")]
41 UnlinkedProject,
42
43 #[error("could not complete the action as your credential file is corrupt")]
45 CorruptCredentialsFile,
46
47 #[error("could not complete the action as your access token is corrupt")]
49 CorruptAccessToken,
50
51 #[error("could not complete the action as your project metadata file are corrupt")]
53 CorruptProjectMetadataFile,
54
55 #[error("unauthorized or deleted user")]
57 UnauthorizedOrDeletedUser,
58
59 #[error("incorrectly scoped token")]
61 IncorrectlyScopedToken,
62
63 #[error("could not read the project graphql schema")]
65 ReadSchema,
66
67 #[error("could not write the project metadata file\ncaused by: {0}")]
69 WriteProjectMetadataFile(io::Error),
70
71 #[error("could not complete a request")]
73 RequestError,
74
75 #[error("could not complete a request")]
77 ConnectionError,
78
79 #[error("could not proceed as this local project has already been linked to a remote project")]
81 ProjectAlreadyLinked,
82
83 #[error("could not find the current user home folder")]
85 FindUserDotGrafbaseFolder,
86
87 #[error("could not create '~/.grafbase'\ncaused by: {0}")]
89 CreateUserDotGrafbaseFolder(io::Error),
90
91 #[error("could not create '.grafbase'\ncaused by: {0}")]
93 CreateProjectDotGrafbaseFolder(io::Error),
94
95 #[error("could not find an available port")]
97 FindAvailablePort,
98
99 #[error("could not complete the request to upload the deployment archive")]
101 UploadError,
102
103 #[error("could not read the upload archive metadata\ncaused by: {0}")]
105 ReadArchiveMetadata(io::Error),
106
107 #[error("could not read the upload archive\ncaused by: {0}")]
109 ReadArchive(io::Error),
110
111 #[error("could not append a file or directory to the upload archive\ncaused by: {0}")]
113 AppendToArchive(io::Error),
114
115 #[error("could not create a temporary file\ncaused by: {0}")]
117 CreateTempFile(io::Error),
118
119 #[error(transparent)]
121 CreateError(#[from] CreateError),
122
123 #[error(transparent)]
125 DeployError(#[from] DeployError),
126}
127
128#[derive(Error, Debug)]
129pub enum CreateError {
130 #[error("could not create a new project as the provided slug is already in use")]
132 SlugAlreadyExists,
133
134 #[error("could not create a new project as the provided slug is invalid")]
136 SlugInvalid,
137
138 #[error("could not create a new project as the provided slug is longer than {max_length} characters")]
140 SlugTooLong { max_length: i32 },
141
142 #[error("could not create a new project as the specified account ID does not exist")]
144 AccountDoesNotExist,
145
146 #[error("could not create a new project as the current plan limit of {max} projects has been reached")]
148 CurrentPlanLimitReached { max: i32 },
149
150 #[error("could not create a new project as duplicate database regions were selected")]
152 DuplicateDatabaseRegions { duplicates: Vec<String> },
153
154 #[error("could not create a new project as no database regions were selected")]
156 EmptyDatabaseRegions,
157
158 #[error("could not create a new project as invalid regions were selected")]
160 InvalidDatabaseRegions { invalid: Vec<String> },
161
162 #[error("could not create a new project, encountered an unknown error")]
164 Unknown,
165}
166
167#[derive(Error, Debug)]
168pub enum DeployError {
169 #[error("could not deploy as the linked project does not exist")]
171 ProjectDoesNotExist,
172
173 #[error("could not deploy as the created archive size is above the allowed limit of {limit} bytes")]
175 ArchiveFileSizeLimitExceeded { limit: i32 },
176
177 #[error("could not deploy as you have reached the allowed daily deployemnt amount of {limit}")]
179 DailyDeploymentCountLimitExceeded { limit: i32 },
180
181 #[error("could not deploy, encountered an unknown error")]
183 Unknown,
184}
185
186#[derive(Error, Debug)]
187pub enum LoginApiError {
188 #[error("could not write '{0}'")]
189 WriteCredentialFile(PathBuf),
190}
191
192impl From<CynicReqwestError> for ApiError {
193 fn from(error: CynicReqwestError) -> Self {
194 match error {
195 CynicReqwestError::ReqwestError(error) if error.is_connect() => ApiError::ConnectionError,
196 CynicReqwestError::ReqwestError(_) | CynicReqwestError::ErrorResponse(_, _) => ApiError::RequestError,
197 }
198 }
199}