From 701ff5d6bcb02e00639ac3aadb1c87467dfb066b Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Wed, 22 Jan 2025 16:35:06 -0300 Subject: [PATCH] refactor(edgestacks): move handlerDBErr() out of the handler BE-11572 (#336) --- api/http/handler/edgestacks/edgestack_file.go | 2 +- api/http/handler/edgestacks/edgestack_inspect.go | 2 +- api/http/handler/edgestacks/edgestack_status_delete.go | 2 +- api/http/handler/edgestacks/edgestack_status_update.go | 4 ++-- api/http/handler/edgestacks/edgestack_update.go | 2 +- api/http/handler/edgestacks/handler.go | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/api/http/handler/edgestacks/edgestack_file.go b/api/http/handler/edgestacks/edgestack_file.go index 3eb6e77e1..70551b83f 100644 --- a/api/http/handler/edgestacks/edgestack_file.go +++ b/api/http/handler/edgestacks/edgestack_file.go @@ -34,7 +34,7 @@ func (handler *Handler) edgeStackFile(w http.ResponseWriter, r *http.Request) *h stack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(stackID)) if err != nil { - return handler.handlerDBErr(err, "Unable to find an edge stack with the specified identifier inside the database") + return handlerDBErr(err, "Unable to find an edge stack with the specified identifier inside the database") } fileName := stack.EntryPoint diff --git a/api/http/handler/edgestacks/edgestack_inspect.go b/api/http/handler/edgestacks/edgestack_inspect.go index a19b70a0e..06c118835 100644 --- a/api/http/handler/edgestacks/edgestack_inspect.go +++ b/api/http/handler/edgestacks/edgestack_inspect.go @@ -30,7 +30,7 @@ func (handler *Handler) edgeStackInspect(w http.ResponseWriter, r *http.Request) edgeStack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(edgeStackID)) if err != nil { - return handler.handlerDBErr(err, "Unable to find an edge stack with the specified identifier inside the database") + return handlerDBErr(err, "Unable to find an edge stack with the specified identifier inside the database") } return response.JSON(w, edgeStack) diff --git a/api/http/handler/edgestacks/edgestack_status_delete.go b/api/http/handler/edgestacks/edgestack_status_delete.go index d63d5bb93..eb54951b6 100644 --- a/api/http/handler/edgestacks/edgestack_status_delete.go +++ b/api/http/handler/edgestacks/edgestack_status_delete.go @@ -63,7 +63,7 @@ func (handler *Handler) edgeStackStatusDelete(w http.ResponseWriter, r *http.Req func (handler *Handler) deleteEdgeStackStatus(tx dataservices.DataStoreTx, stackID portainer.EdgeStackID, endpoint *portainer.Endpoint) (*portainer.EdgeStack, error) { stack, err := tx.EdgeStack().EdgeStack(stackID) if err != nil { - return nil, handler.handlerDBErr(err, "Unable to find a stack with the specified identifier inside the database") + return nil, handlerDBErr(err, "Unable to find a stack with the specified identifier inside the database") } environmentStatus, ok := stack.Status[endpoint.ID] diff --git a/api/http/handler/edgestacks/edgestack_status_update.go b/api/http/handler/edgestacks/edgestack_status_update.go index 6a8b1e535..ab6ffd8a3 100644 --- a/api/http/handler/edgestacks/edgestack_status_update.go +++ b/api/http/handler/edgestacks/edgestack_status_update.go @@ -116,7 +116,7 @@ func (handler *Handler) updateEdgeStackStatus(tx dataservices.DataStoreTx, r *ht endpoint, err := tx.Endpoint().Endpoint(payload.EndpointID) if err != nil { - return nil, handler.handlerDBErr(fmt.Errorf("unable to find the environment from the database: %w. Environment ID: %d", err, payload.EndpointID), "unable to find the environment") + return nil, handlerDBErr(fmt.Errorf("unable to find the environment from the database: %w. Environment ID: %d", err, payload.EndpointID), "unable to find the environment") } if err := handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint); err != nil { @@ -139,7 +139,7 @@ func (handler *Handler) updateEdgeStackStatus(tx dataservices.DataStoreTx, r *ht updateEnvStatus(payload.EndpointID, stack, deploymentStatus) if err := tx.EdgeStack().UpdateEdgeStack(stackID, stack); err != nil { - return nil, handler.handlerDBErr(fmt.Errorf("unable to update Edge stack to the database: %w. Environment name: %s", err, endpoint.Name), "unable to update Edge stack") + return nil, handlerDBErr(fmt.Errorf("unable to update Edge stack to the database: %w. Environment name: %s", err, endpoint.Name), "unable to update Edge stack") } return stack, nil diff --git a/api/http/handler/edgestacks/edgestack_update.go b/api/http/handler/edgestacks/edgestack_update.go index 033cecabd..9b5bb623d 100644 --- a/api/http/handler/edgestacks/edgestack_update.go +++ b/api/http/handler/edgestacks/edgestack_update.go @@ -80,7 +80,7 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) func (handler *Handler) updateEdgeStack(tx dataservices.DataStoreTx, stackID portainer.EdgeStackID, payload updateEdgeStackPayload) (*portainer.EdgeStack, error) { stack, err := tx.EdgeStack().EdgeStack(stackID) if err != nil { - return nil, handler.handlerDBErr(err, "Unable to find a stack with the specified identifier inside the database") + return nil, handlerDBErr(err, "Unable to find a stack with the specified identifier inside the database") } relationConfig, err := edge.FetchEndpointRelationsConfig(tx) diff --git a/api/http/handler/edgestacks/handler.go b/api/http/handler/edgestacks/handler.go index ad9bff3b8..dd3b5593d 100644 --- a/api/http/handler/edgestacks/handler.go +++ b/api/http/handler/edgestacks/handler.go @@ -58,10 +58,10 @@ func NewHandler(bouncer security.BouncerService, dataStore dataservices.DataStor return h } -func (handler *Handler) handlerDBErr(err error, msg string) *httperror.HandlerError { +func handlerDBErr(err error, msg string) *httperror.HandlerError { httpErr := httperror.InternalServerError(msg, err) - if handler.DataStore.IsErrObjectNotFound(err) { + if dataservices.IsErrObjectNotFound(err) { httpErr.StatusCode = http.StatusNotFound }