feat: handle workspace conflict error
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ContainerSSHHostPort returns the port on the host that is exposing the internal ssh port of the given container info
|
||||
@@ -33,3 +34,8 @@ func ContainerHostPort(ctx context.Context, container types.ContainerJSON, port
|
||||
}
|
||||
return port
|
||||
}
|
||||
|
||||
// CleanErrorMessage removes unnecessary parts in a docker sdk error message, such as "Error response from daemon:"
|
||||
func CleanErrorMessage(msg string) string {
|
||||
return strings.Replace(msg, "Error response from daemon: ", "", 1)
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/docker/docker/errdefs"
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
"strings"
|
||||
"tesseract/internal/docker"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -259,7 +259,7 @@ func (mgr *templateManager) buildTemplate(ctx context.Context, template *templat
|
||||
// the docker sdk returns an error message that looks like:
|
||||
// "Error response from daemon: dockerfile parse error on line 1: unknown instruction: FR (did you mean FROM?)"
|
||||
// we don't want the "error response..." part because it is meaningless
|
||||
message: strings.Replace(err.Error(), "Error response from daemon: ", "", 1),
|
||||
message: docker.CleanErrorMessage(err.Error()),
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
|
9
internal/workspace/errors.go
Normal file
9
internal/workspace/errors.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package workspace
|
||||
|
||||
type errWorkspaceExists struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (err *errWorkspaceExists) Error() string {
|
||||
return err.message
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user