Procházet zdrojové kódy

case on status not found for desired and current calls

Alexander Belanger před 4 roky
rodič
revize
71f3cbe94e

+ 9 - 1
api/server/handlers/infra/get_current.go

@@ -1,6 +1,7 @@
 package infra
 package infra
 
 
 import (
 import (
+	"errors"
 	"net/http"
 	"net/http"
 
 
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/handlers"
@@ -34,7 +35,14 @@ func (c *InfraGetCurrentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 	// get the unique infra name and query from the TF HTTP backend
 	// get the unique infra name and query from the TF HTTP backend
 	current, err := client.GetCurrentState(infra.GetUniqueName())
 	current, err := client.GetCurrentState(infra.GetUniqueName())
 
 
-	if err != nil {
+	if err != nil && errors.Is(err, httpbackend.ErrNotFound) {
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+			err,
+			http.StatusNotFound,
+		))
+
+		return
+	} else if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
 		return
 	}
 	}

+ 9 - 1
api/server/handlers/infra/get_desired.go

@@ -1,6 +1,7 @@
 package infra
 package infra
 
 
 import (
 import (
+	"errors"
 	"net/http"
 	"net/http"
 
 
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/handlers"
@@ -34,7 +35,14 @@ func (c *InfraGetDesiredHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 	// get the unique infra name and query from the TF HTTP backend
 	// get the unique infra name and query from the TF HTTP backend
 	desired, err := client.GetDesiredState(infra.GetUniqueName())
 	desired, err := client.GetDesiredState(infra.GetUniqueName())
 
 
-	if err != nil {
+	if err != nil && errors.Is(err, httpbackend.ErrNotFound) {
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+			err,
+			http.StatusNotFound,
+		))
+
+		return
+	} else if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
 		return
 	}
 	}

+ 6 - 0
ee/integrations/httpbackend/backend.go

@@ -46,6 +46,8 @@ func (c *Client) GetDesiredState(name string) (*DesiredTFState, error) {
 	return resp.Data, nil
 	return resp.Data, nil
 }
 }
 
 
+var ErrNotFound = fmt.Errorf("Not found")
+
 func (c *Client) getRequest(path string, dst interface{}) error {
 func (c *Client) getRequest(path string, dst interface{}) error {
 	req, err := http.NewRequest(
 	req, err := http.NewRequest(
 		"GET",
 		"GET",
@@ -75,6 +77,10 @@ func (c *Client) getRequest(path string, dst interface{}) error {
 			return fmt.Errorf("request failed with status code %d, but could not read body (%s)\n", res.StatusCode, err.Error())
 			return fmt.Errorf("request failed with status code %d, but could not read body (%s)\n", res.StatusCode, err.Error())
 		}
 		}
 
 
+		if res.StatusCode == http.StatusNotFound {
+			return ErrNotFound
+		}
+
 		return fmt.Errorf("request failed with status code %d: %s\n", res.StatusCode, string(resBytes))
 		return fmt.Errorf("request failed with status code %d: %s\n", res.StatusCode, string(resBytes))
 	}
 	}