fix(api): fix issues with old error declaration

pull/4016/head
Anthony Lapenna 2020-07-08 12:25:37 +12:00
parent db4a5292be
commit 08095913a6
5 changed files with 37 additions and 28 deletions

View File

@ -85,19 +85,19 @@ type customTemplateFromFileContentPayload struct {
func (payload *customTemplateFromFileContentPayload) Validate(r *http.Request) error { func (payload *customTemplateFromFileContentPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Title) { if govalidator.IsNull(payload.Title) {
return portainer.Error("Invalid custom template title") return errors.New("Invalid custom template title")
} }
if govalidator.IsNull(payload.Description) { if govalidator.IsNull(payload.Description) {
return portainer.Error("Invalid custom template description") return errors.New("Invalid custom template description")
} }
if govalidator.IsNull(payload.FileContent) { 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 { 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 { 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 return nil
} }
@ -148,25 +148,25 @@ type customTemplateFromGitRepositoryPayload struct {
func (payload *customTemplateFromGitRepositoryPayload) Validate(r *http.Request) error { func (payload *customTemplateFromGitRepositoryPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Title) { if govalidator.IsNull(payload.Title) {
return portainer.Error("Invalid custom template title") return errors.New("Invalid custom template title")
} }
if govalidator.IsNull(payload.Description) { 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) { 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)) { 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) { if govalidator.IsNull(payload.ComposeFilePathInRepository) {
payload.ComposeFilePathInRepository = filesystem.ComposeFileDefaultName payload.ComposeFilePathInRepository = filesystem.ComposeFileDefaultName
} }
if payload.Platform != portainer.CustomTemplatePlatformLinux && payload.Platform != portainer.CustomTemplatePlatformWindows { 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 { 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 return nil
} }
@ -223,13 +223,13 @@ type customTemplateFromFileUploadPayload struct {
func (payload *customTemplateFromFileUploadPayload) Validate(r *http.Request) error { func (payload *customTemplateFromFileUploadPayload) Validate(r *http.Request) error {
title, err := request.RetrieveMultiPartFormValue(r, "Title", false) title, err := request.RetrieveMultiPartFormValue(r, "Title", false)
if err != nil { if err != nil {
return portainer.Error("Invalid custom template title") return errors.New("Invalid custom template title")
} }
payload.Title = title payload.Title = title
description, err := request.RetrieveMultiPartFormValue(r, "Description", false) description, err := request.RetrieveMultiPartFormValue(r, "Description", false)
if err != nil { if err != nil {
return portainer.Error("Invalid custom template description") return errors.New("Invalid custom template description")
} }
payload.Description = description payload.Description = description
@ -240,20 +240,20 @@ func (payload *customTemplateFromFileUploadPayload) Validate(r *http.Request) er
platform, _ := request.RetrieveNumericMultiPartFormValue(r, "Platform", true) platform, _ := request.RetrieveNumericMultiPartFormValue(r, "Platform", true)
templatePlatform := portainer.CustomTemplatePlatform(platform) templatePlatform := portainer.CustomTemplatePlatform(platform)
if templatePlatform != portainer.CustomTemplatePlatformLinux && templatePlatform != portainer.CustomTemplatePlatformWindows { if templatePlatform != portainer.CustomTemplatePlatformLinux && templatePlatform != portainer.CustomTemplatePlatformWindows {
return portainer.Error("Invalid custom template platform") return errors.New("Invalid custom template platform")
} }
payload.Platform = templatePlatform payload.Platform = templatePlatform
typeNumeral, _ := request.RetrieveNumericMultiPartFormValue(r, "Type", true) typeNumeral, _ := request.RetrieveNumericMultiPartFormValue(r, "Type", true)
templateType := portainer.StackType(typeNumeral) templateType := portainer.StackType(typeNumeral)
if templateType != portainer.DockerComposeStack && templateType != portainer.DockerSwarmStack { if templateType != portainer.DockerComposeStack && templateType != portainer.DockerSwarmStack {
return portainer.Error("Invalid custom template type") return errors.New("Invalid custom template type")
} }
payload.Type = templateType payload.Type = templateType
composeFileContent, _, err := request.RetrieveMultiPartFormFile(r, "file") composeFileContent, _, err := request.RetrieveMultiPartFormFile(r, "file")
if err != nil { 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 payload.FileContent = composeFileContent

View File

@ -8,6 +8,8 @@ import (
"github.com/portainer/libhttp/request" "github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response" "github.com/portainer/libhttp/response"
portainer "github.com/portainer/portainer/api" 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" "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)) 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} return &httperror.HandlerError{http.StatusNotFound, "Unable to find a custom template with the specified identifier inside the database", err}
} else if err != nil { } else if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a custom template with the specified identifier inside the database", err} 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) access := userCanEditTemplate(customTemplate, securityContext)
if !access { 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)) err = handler.DataStore.CustomTemplate().DeleteCustomTemplate(portainer.CustomTemplateID(customTemplateID))

View File

@ -8,6 +8,7 @@ import (
"github.com/portainer/libhttp/request" "github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response" "github.com/portainer/libhttp/response"
"github.com/portainer/portainer/api" "github.com/portainer/portainer/api"
bolterrors "github.com/portainer/portainer/api/bolt/errors"
) )
type fileResponse struct { 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)) 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} return &httperror.HandlerError{http.StatusNotFound, "Unable to find a custom template with the specified identifier inside the database", err}
} else if err != nil { } else if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a custom template with the specified identifier inside the database", err} return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a custom template with the specified identifier inside the database", err}

View File

@ -8,6 +8,8 @@ import (
"github.com/portainer/libhttp/request" "github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response" "github.com/portainer/libhttp/response"
"github.com/portainer/portainer/api" "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" "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)) 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} return &httperror.HandlerError{http.StatusNotFound, "Unable to find a custom template with the specified identifier inside the database", err}
} else if err != nil { } else if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a custom template with the specified identifier inside the database", err} 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) access := userCanEditTemplate(customTemplate, securityContext)
if !access { 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 { if resourceControl != nil {

View File

@ -1,14 +1,18 @@
package customtemplates package customtemplates
import ( import (
"errors"
"net/http" "net/http"
"strconv" "strconv"
bolterrors "github.com/portainer/portainer/api/bolt/errors"
"github.com/asaskevich/govalidator" "github.com/asaskevich/govalidator"
httperror "github.com/portainer/libhttp/error" httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/request" "github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response" "github.com/portainer/libhttp/response"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
httperrors "github.com/portainer/portainer/api/http/errors"
"github.com/portainer/portainer/api/http/security" "github.com/portainer/portainer/api/http/security"
) )
@ -24,19 +28,19 @@ type customTemplateUpdatePayload struct {
func (payload *customTemplateUpdatePayload) Validate(r *http.Request) error { func (payload *customTemplateUpdatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Title) { if govalidator.IsNull(payload.Title) {
return portainer.Error("Invalid custom template title") return errors.New("Invalid custom template title")
} }
if govalidator.IsNull(payload.FileContent) { 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 { 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 { 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) { if govalidator.IsNull(payload.Description) {
return portainer.Error("Invalid custom template description") return errors.New("Invalid custom template description")
} }
return nil 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)) 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} return &httperror.HandlerError{http.StatusNotFound, "Unable to find a custom template with the specified identifier inside the database", err}
} else if err != nil { } else if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a custom template with the specified identifier inside the database", err} 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) access := userCanEditTemplate(customTemplate, securityContext)
if !access { 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) templateFolder := strconv.Itoa(customTemplateID)