feat: handle template build error

This commit is contained in:
2024-12-02 13:45:49 +00:00
parent 8a8582e06e
commit dc97c2c498
9 changed files with 123 additions and 11 deletions

View File

@@ -0,0 +1,17 @@
package apierror
import "fmt"
type APIError struct {
StatusCode int `json:"-"`
Code string `json:"code"`
Message string `json:"error"`
}
func New(status int, code, message string) *APIError {
return &APIError{status, code, message}
}
func (err *APIError) Error() string {
return fmt.Sprintf("%s: %s", err.Code, err.Message)
}

View File

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

View File

@@ -7,6 +7,7 @@ import (
"github.com/labstack/echo/v4"
"io"
"net/http"
"tesseract/internal/apierror"
"tesseract/internal/service"
)
@@ -136,6 +137,10 @@ func buildTemplate(c echo.Context, body postTemplateRequestBody) error {
buildArgs: body.BuildArgs,
})
if err != nil {
var errBadTemplate *errBadTemplate
if errors.As(err, &errBadTemplate) {
return apierror.New(http.StatusBadRequest, "BAD_TEMPLATE", errBadTemplate.message)
}
return err
}

View File

@@ -11,8 +11,10 @@ import (
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/google/uuid"
"github.com/uptrace/bun"
"strings"
"time"
)
@@ -252,6 +254,14 @@ func (mgr *templateManager) buildTemplate(ctx context.Context, template *templat
BuildArgs: opts.buildArgs,
})
if err != nil {
if errdefs.IsInvalidParameter(err) {
return nil, &errBadTemplate{
// 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),
}
}
return nil, err
}