feat: handle workspace conflict error

This commit is contained in:
2024-12-02 19:12:26 +00:00
parent 4c34689ceb
commit 5fa55493b7
12 changed files with 74 additions and 19 deletions

View File

@@ -0,0 +1,9 @@
package workspace
type errWorkspaceExists struct {
message string
}
func (err *errWorkspaceExists) Error() string {
return err.message
}

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/labstack/echo/v4"
"net/http"
"tesseract/internal/apierror"
)
type createWorkspaceRequestBody struct {
@@ -86,6 +87,12 @@ func createWorkspace(c echo.Context, workspaceName string) error {
if errors.Is(err, errImageNotFound) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("no image with id %v exists", body.ImageID))
}
var errWorkspaceExists *errWorkspaceExists
if errors.As(err, &errWorkspaceExists) {
return apierror.New(http.StatusBadRequest, "WORKSPACE_EXISTS", errWorkspaceExists.message)
}
return err
}

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/docker/go-connections/nat"
"github.com/google/uuid"
"github.com/uptrace/bun"
@@ -174,6 +175,11 @@ func (mgr workspaceManager) createWorkspace(ctx context.Context, opts createWork
res, err := mgr.dockerClient.ContainerCreate(ctx, containerConfig, hostConfig, nil, nil, opts.name)
if err != nil {
if errdefs.IsConflict(err) {
return nil, &errWorkspaceExists{
message: docker.CleanErrorMessage(err.Error()),
}
}
return nil, err
}