From 08095913a68f0915e5ef2f147d417c7496a58a41 Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Wed, 8 Jul 2020 12:25:37 +1200 Subject: [PATCH] fix(api): fix issues with old error declaration --- .../customtemplates/customtemplate_create.go | 32 +++++++++---------- .../customtemplates/customtemplate_delete.go | 6 ++-- .../customtemplates/customtemplate_file.go | 3 +- .../customtemplates/customtemplate_inspect.go | 6 ++-- .../customtemplates/customtemplate_update.go | 18 +++++++---- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/api/http/handler/customtemplates/customtemplate_create.go b/api/http/handler/customtemplates/customtemplate_create.go index 3bb8fef0b..af1a81e48 100644 --- a/api/http/handler/customtemplates/customtemplate_create.go +++ b/api/http/handler/customtemplates/customtemplate_create.go @@ -85,19 +85,19 @@ type customTemplateFromFileContentPayload struct { func (payload *customTemplateFromFileContentPayload) Validate(r *http.Request) error { if govalidator.IsNull(payload.Title) { - return portainer.Error("Invalid custom template title") + return errors.New("Invalid custom template title") } if govalidator.IsNull(payload.Description) { - return portainer.Error("Invalid custom template description") + return errors.New("Invalid custom template description") } if govalidator.IsNull(payload.FileContent) { - return portainer.Error("Invalid file content") + return errors.New("Invalid file content") } if payload.Platform != portainer.CustomTemplatePlatformLinux && payload.Platform != portainer.CustomTemplatePlatformWindows { - return portainer.Error("Invalid custom template platform") + return errors.New("Invalid custom template platform") } if payload.Type != portainer.DockerSwarmStack && payload.Type != portainer.DockerComposeStack { - return portainer.Error("Invalid custom template type") + return errors.New("Invalid custom template type") } return nil } @@ -148,25 +148,25 @@ type customTemplateFromGitRepositoryPayload struct { func (payload *customTemplateFromGitRepositoryPayload) Validate(r *http.Request) error { if govalidator.IsNull(payload.Title) { - return portainer.Error("Invalid custom template title") + return errors.New("Invalid custom template title") } if govalidator.IsNull(payload.Description) { - return portainer.Error("Invalid custom template description") + return errors.New("Invalid custom template description") } if govalidator.IsNull(payload.RepositoryURL) || !govalidator.IsURL(payload.RepositoryURL) { - return portainer.Error("Invalid repository URL. Must correspond to a valid URL format") + return errors.New("Invalid repository URL. Must correspond to a valid URL format") } if payload.RepositoryAuthentication && (govalidator.IsNull(payload.RepositoryUsername) || govalidator.IsNull(payload.RepositoryPassword)) { - return portainer.Error("Invalid repository credentials. Username and password must be specified when authentication is enabled") + return errors.New("Invalid repository credentials. Username and password must be specified when authentication is enabled") } if govalidator.IsNull(payload.ComposeFilePathInRepository) { payload.ComposeFilePathInRepository = filesystem.ComposeFileDefaultName } if payload.Platform != portainer.CustomTemplatePlatformLinux && payload.Platform != portainer.CustomTemplatePlatformWindows { - return portainer.Error("Invalid custom template platform") + return errors.New("Invalid custom template platform") } if payload.Type != portainer.DockerSwarmStack && payload.Type != portainer.DockerComposeStack { - return portainer.Error("Invalid custom template type") + return errors.New("Invalid custom template type") } return nil } @@ -223,13 +223,13 @@ type customTemplateFromFileUploadPayload struct { func (payload *customTemplateFromFileUploadPayload) Validate(r *http.Request) error { title, err := request.RetrieveMultiPartFormValue(r, "Title", false) if err != nil { - return portainer.Error("Invalid custom template title") + return errors.New("Invalid custom template title") } payload.Title = title description, err := request.RetrieveMultiPartFormValue(r, "Description", false) if err != nil { - return portainer.Error("Invalid custom template description") + return errors.New("Invalid custom template description") } payload.Description = description @@ -240,20 +240,20 @@ func (payload *customTemplateFromFileUploadPayload) Validate(r *http.Request) er platform, _ := request.RetrieveNumericMultiPartFormValue(r, "Platform", true) templatePlatform := portainer.CustomTemplatePlatform(platform) if templatePlatform != portainer.CustomTemplatePlatformLinux && templatePlatform != portainer.CustomTemplatePlatformWindows { - return portainer.Error("Invalid custom template platform") + return errors.New("Invalid custom template platform") } payload.Platform = templatePlatform typeNumeral, _ := request.RetrieveNumericMultiPartFormValue(r, "Type", true) templateType := portainer.StackType(typeNumeral) if templateType != portainer.DockerComposeStack && templateType != portainer.DockerSwarmStack { - return portainer.Error("Invalid custom template type") + return errors.New("Invalid custom template type") } payload.Type = templateType composeFileContent, _, err := request.RetrieveMultiPartFormFile(r, "file") if err != nil { - return portainer.Error("Invalid Compose file. Ensure that the Compose file is uploaded correctly") + return errors.New("Invalid Compose file. Ensure that the Compose file is uploaded correctly") } payload.FileContent = composeFileContent diff --git a/api/http/handler/customtemplates/customtemplate_delete.go b/api/http/handler/customtemplates/customtemplate_delete.go index f81c41992..24500894a 100644 --- a/api/http/handler/customtemplates/customtemplate_delete.go +++ b/api/http/handler/customtemplates/customtemplate_delete.go @@ -8,6 +8,8 @@ import ( "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" portainer "github.com/portainer/portainer/api" + bolterrors "github.com/portainer/portainer/api/bolt/errors" + httperrors "github.com/portainer/portainer/api/http/errors" "github.com/portainer/portainer/api/http/security" ) @@ -23,7 +25,7 @@ func (handler *Handler) customTemplateDelete(w http.ResponseWriter, r *http.Requ } customTemplate, err := handler.DataStore.CustomTemplate().CustomTemplate(portainer.CustomTemplateID(customTemplateID)) - if err == portainer.ErrObjectNotFound { + if err == bolterrors.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a custom template with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a custom template with the specified identifier inside the database", err} @@ -36,7 +38,7 @@ func (handler *Handler) customTemplateDelete(w http.ResponseWriter, r *http.Requ access := userCanEditTemplate(customTemplate, securityContext) if !access { - return &httperror.HandlerError{http.StatusForbidden, "Access denied to resource", portainer.ErrResourceAccessDenied} + return &httperror.HandlerError{http.StatusForbidden, "Access denied to resource", httperrors.ErrResourceAccessDenied} } err = handler.DataStore.CustomTemplate().DeleteCustomTemplate(portainer.CustomTemplateID(customTemplateID)) diff --git a/api/http/handler/customtemplates/customtemplate_file.go b/api/http/handler/customtemplates/customtemplate_file.go index 5b3689fa1..5fa74d813 100644 --- a/api/http/handler/customtemplates/customtemplate_file.go +++ b/api/http/handler/customtemplates/customtemplate_file.go @@ -8,6 +8,7 @@ import ( "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" "github.com/portainer/portainer/api" + bolterrors "github.com/portainer/portainer/api/bolt/errors" ) type fileResponse struct { @@ -22,7 +23,7 @@ func (handler *Handler) customTemplateFile(w http.ResponseWriter, r *http.Reques } customTemplate, err := handler.DataStore.CustomTemplate().CustomTemplate(portainer.CustomTemplateID(customTemplateID)) - if err == portainer.ErrObjectNotFound { + if err == bolterrors.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a custom template with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a custom template with the specified identifier inside the database", err} diff --git a/api/http/handler/customtemplates/customtemplate_inspect.go b/api/http/handler/customtemplates/customtemplate_inspect.go index fd77be2b0..6259e3eb7 100644 --- a/api/http/handler/customtemplates/customtemplate_inspect.go +++ b/api/http/handler/customtemplates/customtemplate_inspect.go @@ -8,6 +8,8 @@ import ( "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" "github.com/portainer/portainer/api" + bolterrors "github.com/portainer/portainer/api/bolt/errors" + httperrors "github.com/portainer/portainer/api/http/errors" "github.com/portainer/portainer/api/http/security" ) @@ -18,7 +20,7 @@ func (handler *Handler) customTemplateInspect(w http.ResponseWriter, r *http.Req } customTemplate, err := handler.DataStore.CustomTemplate().CustomTemplate(portainer.CustomTemplateID(customTemplateID)) - if err == portainer.ErrObjectNotFound { + if err == bolterrors.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a custom template with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a custom template with the specified identifier inside the database", err} @@ -36,7 +38,7 @@ func (handler *Handler) customTemplateInspect(w http.ResponseWriter, r *http.Req access := userCanEditTemplate(customTemplate, securityContext) if !access { - return &httperror.HandlerError{http.StatusForbidden, "Access denied to resource", portainer.ErrResourceAccessDenied} + return &httperror.HandlerError{http.StatusForbidden, "Access denied to resource", httperrors.ErrResourceAccessDenied} } if resourceControl != nil { diff --git a/api/http/handler/customtemplates/customtemplate_update.go b/api/http/handler/customtemplates/customtemplate_update.go index b969b4fe2..51939833f 100644 --- a/api/http/handler/customtemplates/customtemplate_update.go +++ b/api/http/handler/customtemplates/customtemplate_update.go @@ -1,14 +1,18 @@ package customtemplates import ( + "errors" "net/http" "strconv" + bolterrors "github.com/portainer/portainer/api/bolt/errors" + "github.com/asaskevich/govalidator" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" portainer "github.com/portainer/portainer/api" + httperrors "github.com/portainer/portainer/api/http/errors" "github.com/portainer/portainer/api/http/security" ) @@ -24,19 +28,19 @@ type customTemplateUpdatePayload struct { func (payload *customTemplateUpdatePayload) Validate(r *http.Request) error { if govalidator.IsNull(payload.Title) { - return portainer.Error("Invalid custom template title") + return errors.New("Invalid custom template title") } if govalidator.IsNull(payload.FileContent) { - return portainer.Error("Invalid file content") + return errors.New("Invalid file content") } if payload.Platform != portainer.CustomTemplatePlatformLinux && payload.Platform != portainer.CustomTemplatePlatformWindows { - return portainer.Error("Invalid custom template platform") + return errors.New("Invalid custom template platform") } if payload.Type != portainer.DockerComposeStack && payload.Type != portainer.DockerSwarmStack { - return portainer.Error("Invalid custom template type") + return errors.New("Invalid custom template type") } if govalidator.IsNull(payload.Description) { - return portainer.Error("Invalid custom template description") + return errors.New("Invalid custom template description") } return nil } @@ -54,7 +58,7 @@ func (handler *Handler) customTemplateUpdate(w http.ResponseWriter, r *http.Requ } customTemplate, err := handler.DataStore.CustomTemplate().CustomTemplate(portainer.CustomTemplateID(customTemplateID)) - if err == portainer.ErrObjectNotFound { + if err == bolterrors.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a custom template with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a custom template with the specified identifier inside the database", err} @@ -67,7 +71,7 @@ func (handler *Handler) customTemplateUpdate(w http.ResponseWriter, r *http.Requ access := userCanEditTemplate(customTemplate, securityContext) if !access { - return &httperror.HandlerError{http.StatusForbidden, "Access denied to resource", portainer.ErrResourceAccessDenied} + return &httperror.HandlerError{http.StatusForbidden, "Access denied to resource", httperrors.ErrResourceAccessDenied} } templateFolder := strconv.Itoa(customTemplateID)