From 5fe5ae388d200ca2c9e59c8c0e4540ae0f9554b7 Mon Sep 17 00:00:00 2001 From: fiftin Date: Tue, 2 Apr 2024 23:10:42 +0200 Subject: [PATCH] refactor(errors): add method WriteErrorStatus --- api/helpers/helpers.go | 15 ++++++++++----- api/projects/inventory.go | 19 +++++++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/api/helpers/helpers.go b/api/helpers/helpers.go index 32e9c8c8..bd5f9511 100644 --- a/api/helpers/helpers.go +++ b/api/helpers/helpers.go @@ -2,6 +2,7 @@ package helpers import ( "encoding/json" + "errors" "fmt" "github.com/ansible-semaphore/semaphore/services/tasks" "net/http" @@ -90,22 +91,26 @@ func WriteJSON(w http.ResponseWriter, code int, out interface{}) { } } +func WriteErrorStatus(w http.ResponseWriter, err string, code int) { + WriteJSON(w, code, map[string]string{ + "error": err, + }) +} + func WriteError(w http.ResponseWriter, err error) { - if err == db.ErrNotFound { + if errors.Is(err, db.ErrNotFound) { w.WriteHeader(http.StatusNotFound) return } - if err == db.ErrInvalidOperation { + if errors.Is(err, db.ErrInvalidOperation) { w.WriteHeader(http.StatusConflict) return } switch e := err.(type) { case *db.ValidationError: - WriteJSON(w, http.StatusBadRequest, map[string]string{ - "error": e.Error(), - }) + WriteErrorStatus(w, e.Error(), http.StatusBadRequest) default: log.Error(err) debug.PrintStack() diff --git a/api/projects/inventory.go b/api/projects/inventory.go index 3d04dec4..67422062 100644 --- a/api/projects/inventory.go +++ b/api/projects/inventory.go @@ -1,6 +1,7 @@ package projects import ( + "errors" "github.com/ansible-semaphore/semaphore/api/helpers" "github.com/ansible-semaphore/semaphore/db" log "github.com/sirupsen/logrus" @@ -156,16 +157,16 @@ func UpdateInventory(w http.ResponseWriter, r *http.Request) { } if inventory.ID != oldInventory.ID { - helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{ - "error": "Inventory ID in body and URL must be the same", - }) + helpers.WriteErrorStatus(w, + "Inventory ID in body and URL must be the same", + http.StatusBadRequest) return } if inventory.ProjectID != oldInventory.ProjectID { - helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{ - "error": "Project ID in body and URL must be the same", - }) + helpers.WriteErrorStatus(w, + "project ID in body and URL must be the same", + http.StatusBadRequest) return } @@ -178,7 +179,9 @@ func UpdateInventory(w http.ResponseWriter, r *http.Request) { return } default: - w.WriteHeader(http.StatusBadRequest) + helpers.WriteErrorStatus(w, + "unknown inventory type: %s"+string(inventory.Type), + http.StatusBadRequest) return } @@ -201,7 +204,7 @@ func RemoveInventory(w http.ResponseWriter, r *http.Request) { var err error err = helpers.Store(r).DeleteInventory(inventory.ProjectID, inventory.ID) - if err == db.ErrInvalidOperation { + if errors.Is(err, db.ErrInvalidOperation) { helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{ "error": "Inventory is in use by one or more templates", "inUse": true,