Skip to content

Commit 2c7cab3

Browse files
committed
Return out of handler when detecting error
This commit updates the update/create handlers so that they exit the function after writing a header. There was an error observed of multiple headers being written. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent af7f3fb commit 2c7cab3

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

handlers/deploy.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package handlers
55

66
import (
77
"encoding/json"
8+
"fmt"
89
"io/ioutil"
910
"log"
1011
"net/http"
@@ -36,13 +37,14 @@ func MakeDeployHandler(functionNamespace string, factory k8s.FunctionFactory) ht
3637
request := types.FunctionDeployment{}
3738
err := json.Unmarshal(body, &request)
3839
if err != nil {
39-
w.WriteHeader(http.StatusBadRequest)
40+
wrappedErr := fmt.Errorf("failed to unmarshal request: %s", err.Error())
41+
http.Error(w, wrappedErr.Error(), http.StatusBadRequest)
4042
return
4143
}
4244

4345
if err := ValidateDeployRequest(&request); err != nil {
44-
w.WriteHeader(http.StatusBadRequest)
45-
w.Write([]byte(err.Error()))
46+
wrappedErr := fmt.Errorf("validation failed: %s", err.Error())
47+
http.Error(w, wrappedErr.Error(), http.StatusBadRequest)
4648
return
4749
}
4850

@@ -53,26 +55,27 @@ func MakeDeployHandler(functionNamespace string, factory k8s.FunctionFactory) ht
5355

5456
existingSecrets, err := getSecrets(factory.Client, namespace, request.Secrets)
5557
if err != nil {
56-
w.WriteHeader(http.StatusBadRequest)
57-
w.Write([]byte(err.Error()))
58+
wrappedErr := fmt.Errorf("unable to fetch secrets: %s", err.Error())
59+
http.Error(w, wrappedErr.Error(), http.StatusBadRequest)
5860
return
5961
}
6062

6163
deploymentSpec, specErr := makeDeploymentSpec(request, existingSecrets, factory)
6264

6365
if specErr != nil {
64-
w.WriteHeader(http.StatusBadRequest)
65-
w.Write([]byte(specErr.Error()))
66+
wrappedErr := fmt.Errorf("failed create Deployment spec: %s", specErr.Error())
67+
log.Println(wrappedErr)
68+
http.Error(w, wrappedErr.Error(), http.StatusBadRequest)
6669
return
6770
}
6871

6972
deploy := factory.Client.AppsV1().Deployments(namespace)
7073

7174
_, err = deploy.Create(deploymentSpec)
7275
if err != nil {
73-
log.Println(err)
74-
w.WriteHeader(http.StatusInternalServerError)
75-
w.Write([]byte(err.Error()))
76+
wrappedErr := fmt.Errorf("unable create Deployment: %s", err.Error())
77+
log.Println(wrappedErr)
78+
http.Error(w, wrappedErr.Error(), http.StatusInternalServerError)
7679
return
7780
}
7881

@@ -83,9 +86,9 @@ func MakeDeployHandler(functionNamespace string, factory k8s.FunctionFactory) ht
8386
_, err = service.Create(serviceSpec)
8487

8588
if err != nil {
86-
log.Println(err)
87-
w.WriteHeader(http.StatusInternalServerError)
88-
w.Write([]byte(err.Error()))
89+
wrappedErr := fmt.Errorf("failed create Service: %s", err.Error())
90+
log.Println(wrappedErr)
91+
http.Error(w, wrappedErr.Error(), http.StatusBadRequest)
8992
return
9093
}
9194

handlers/update.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,39 @@ func MakeUpdateHandler(defaultNamespace string, factory k8s.FunctionFactory) htt
3333

3434
if lookupNamespace == "kube-system" {
3535
http.Error(w, "unable to list within the kube-system namespace", http.StatusUnauthorized)
36+
return
3637
}
3738

3839
body, _ := ioutil.ReadAll(r.Body)
3940

4041
request := types.FunctionDeployment{}
4142
err := json.Unmarshal(body, &request)
4243
if err != nil {
43-
w.WriteHeader(http.StatusBadRequest)
44+
wrappedErr := fmt.Errorf("unable to unmarshal request: %s", err.Error())
45+
http.Error(w, wrappedErr.Error(), http.StatusBadRequest)
4446
return
4547
}
4648

4749
annotations := buildAnnotations(request)
4850
if err, status := updateDeploymentSpec(lookupNamespace, factory, request, annotations); err != nil {
4951
if !isNotFound(err) {
5052
log.Printf("error updating deployment: %s\n", err)
53+
5154
return
5255
}
53-
w.WriteHeader(status)
54-
w.Write([]byte(err.Error()))
56+
57+
wrappedErr := fmt.Errorf("unable update Deployment: %s", err.Error())
58+
http.Error(w, wrappedErr.Error(), status)
59+
return
5560
}
5661

5762
if err, status := updateService(lookupNamespace, factory, request, annotations); err != nil {
5863
if !isNotFound(err) {
5964
log.Printf("error updating service: %s\n", err)
6065
}
6166

62-
w.WriteHeader(status)
63-
w.Write([]byte(err.Error()))
67+
wrappedErr := fmt.Errorf("unable update Service: %s", err.Error())
68+
http.Error(w, wrappedErr.Error(), status)
6469
return
6570
}
6671

0 commit comments

Comments
 (0)