Parcourir la source

handle stack not found errors (#3231)

ianedwards il y a 2 ans
Parent
commit
a885ff410a
2 fichiers modifiés avec 19 ajouts et 2 suppressions
  1. 18 1
      api/server/handlers/porter_app/get.go
  2. 1 1
      internal/repository/gorm/porter_app.go

+ 18 - 1
api/server/handlers/porter_app/get.go

@@ -1,6 +1,7 @@
 package porter_app
 
 import (
+	"errors"
 	"net/http"
 
 	"github.com/porter-dev/porter/api/server/authz"
@@ -11,6 +12,8 @@ import (
 	"github.com/porter-dev/porter/api/server/shared/requestutils"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/models"
+	"github.com/porter-dev/porter/internal/telemetry"
+	"gorm.io/gorm"
 )
 
 type GetPorterAppHandler struct {
@@ -28,16 +31,30 @@ func NewGetPorterAppHandler(
 }
 
 func (c *GetPorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	ctx := r.Context()
+	ctx, span := telemetry.NewSpan(r.Context(), "serve-get-porter-app")
+
 	cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
+
 	stackName, reqErr := requestutils.GetURLParamString(r, types.URLParamStackName)
 	if reqErr != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(reqErr, http.StatusBadRequest))
 		return
 	}
 
+	telemetry.WithAttributes(span,
+		telemetry.AttributeKV{Key: "cluster_id", Value: cluster.ID},
+		telemetry.AttributeKV{Key: "project_id", Value: cluster.ProjectID},
+		telemetry.AttributeKV{Key: "stack_name", Value: stackName},
+	)
+
 	app, err := c.Repo().PorterApp().ReadPorterAppByName(cluster.ID, stackName)
 	if err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			err = telemetry.Error(ctx, span, err, "porter app not found")
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusNotFound))
+			return
+		}
+
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
 	}

+ 1 - 1
internal/repository/gorm/porter_app.go

@@ -37,7 +37,7 @@ func (repo *PorterAppRepository) ListPorterAppByClusterID(clusterID uint) ([]*mo
 func (repo *PorterAppRepository) ReadPorterAppByName(clusterID uint, name string) (*models.PorterApp, error) {
 	app := &models.PorterApp{}
 
-	if err := repo.db.Where("cluster_id = ? AND name = ?", clusterID, name).Limit(1).Find(&app).Error; err != nil {
+	if err := repo.db.Where("cluster_id = ? AND name = ?", clusterID, name).Limit(1).First(&app).Error; err != nil {
 		return nil, err
 	}