Skip to content

Commit

Permalink
feat(gateway): special error if own project is already running (#1192)
Browse files Browse the repository at this point in the history
* feat(gateway): special error if own project is already running

The command `cargo shuttle project start` fails if a project
with the same name is already present. The error message that's
printed now tells the caller that their project is already running
if they are the project owner.

Fixes #1155

* improve error message
  • Loading branch information
thass0 authored Sep 1, 2023
1 parent 9dd4bbf commit 5a66ca5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 5 additions & 0 deletions common/src/models/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum ErrorKind {
ProjectNotFound,
InvalidProjectName,
ProjectAlreadyExists,
ProjectAlreadyRunning,
ProjectNotReady,
ProjectUnavailable,
CustomDomainNotFound,
Expand Down Expand Up @@ -97,6 +98,10 @@ impl From<ErrorKind> for ApiError {
StatusCode::BAD_REQUEST,
"a project with the same name already exists",
),
ErrorKind::ProjectAlreadyRunning => (
StatusCode::BAD_REQUEST,
"it looks like your project is already running. You can find out more with `cargo shuttle project status`",
),
ErrorKind::InvalidCustomDomain => (StatusCode::BAD_REQUEST, "invalid custom domain"),
ErrorKind::CustomDomainNotFound => (StatusCode::NOT_FOUND, "custom domain not found"),
ErrorKind::CustomDomainAlreadyExists => {
Expand Down
30 changes: 26 additions & 4 deletions gateway/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,10 @@ impl GatewayService {
state: project,
})
} else {
// Otherwise it already exists
Err(Error::from_kind(ErrorKind::ProjectAlreadyExists))
// Otherwise it already exists. Because the caller of this
// command is the project owner, this means that the project
// is already running.
Err(Error::from_kind(ErrorKind::ProjectAlreadyRunning))
}
} else {
// Check if project name is valid according to new rules if it
Expand Down Expand Up @@ -1009,13 +1011,23 @@ pub mod tests {

// If recreated by the same user
assert!(matches!(
svc.create_project(matrix.clone(), neo, false, 0).await,
svc.create_project(matrix.clone(), neo.clone(), false, 0)
.await,
Ok(FindProjectPayload {
project_id: _,
state: Project::Creating(_),
})
));

// If recreated by the same user again while it's running
assert!(matches!(
svc.create_project(matrix.clone(), neo, false, 0).await,
Err(Error {
kind: ErrorKind::ProjectAlreadyRunning,
..
})
));

let mut work = svc
.new_task()
.project(matrix.clone())
Expand All @@ -1036,13 +1048,23 @@ pub mod tests {

// If recreated by an admin
assert!(matches!(
svc.create_project(matrix, trinity, true, 0).await,
svc.create_project(matrix.clone(), trinity.clone(), true, 0)
.await,
Ok(FindProjectPayload {
project_id: _,
state: Project::Creating(_),
})
));

// If recreated by an adamin again while it's running
assert!(matches!(
svc.create_project(matrix, trinity, true, 0).await,
Err(Error {
kind: ErrorKind::ProjectAlreadyRunning,
..
})
));

Ok(())
}

Expand Down

0 comments on commit 5a66ca5

Please sign in to comment.