feat: handle template build error
This commit is contained in:
17
internal/apierror/api_error.go
Normal file
17
internal/apierror/api_error.go
Normal 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)
|
||||
}
|
9
internal/template/errors.go
Normal file
9
internal/template/errors.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package template
|
||||
|
||||
type errBadTemplate struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (err *errBadTemplate) Error() string {
|
||||
return err.message
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user