Skip to content

Commit

Permalink
Log json.Marshal errors
Browse files Browse the repository at this point in the history
This is part of the review from PR #673 and means that if
any type fails to marshal to JSON, that we have a log line
available.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Aug 21, 2020
1 parent 8a9c15a commit d00b38d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 8 deletions.
10 changes: 9 additions & 1 deletion pkg/handlers/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
glog "k8s.io/klog"
)

// MakeNamespacesLister builds a list of namespaces with an "openfaas" tag, or the default name
Expand All @@ -24,7 +25,14 @@ func MakeNamespacesLister(defaultNamespace string, clientset kubernetes.Interfac

res := ListNamespaces(defaultNamespace, clientset)

out, _ := json.Marshal(res)
out, err := json.Marshal(res)
if err != nil {
glog.Errorf("Failed to list namespaces: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to list namespaces"))
return
}

w.Header().Set("Content-Type", "application/json")

w.WriteHeader(http.StatusOK)
Expand Down
10 changes: 9 additions & 1 deletion pkg/handlers/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
glog "k8s.io/klog"

"github.com/openfaas/faas-netes/pkg/k8s"
)
Expand Down Expand Up @@ -45,7 +46,14 @@ func MakeFunctionReader(defaultNamespace string, clientset *kubernetes.Clientset
return
}

functionBytes, _ := json.Marshal(functions)
functionBytes, err := json.Marshal(functions)
if err != nil {
glog.Errorf("Failed to marshal functions: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to marshal functions"))
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(functionBytes)
Expand Down
10 changes: 9 additions & 1 deletion pkg/handlers/replicas.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/openfaas/faas-provider/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
glog "k8s.io/klog"
)

// MakeReplicaUpdater updates desired count of replicas
Expand Down Expand Up @@ -115,7 +116,14 @@ func MakeReplicaReader(defaultNamespace string, clientset *kubernetes.Clientset)

log.Printf("Read replicas - %s %s, %d/%d\n", functionName, lookupNamespace, function.AvailableReplicas, function.Replicas)

functionBytes, _ := json.Marshal(function)
functionBytes, err := json.Marshal(function)
if err != nil {
glog.Errorf("Failed to marshal function: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to marshal function"))
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(functionBytes)
Expand Down
4 changes: 3 additions & 1 deletion pkg/server/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/openfaas/faas-netes/version"
"github.com/openfaas/faas-provider/types"
glog "k8s.io/klog"
)

// makeInfoHandler provides the system/info endpoint
Expand All @@ -27,8 +28,9 @@ func makeInfoHandler() http.HandlerFunc {

infoBytes, err := json.Marshal(info)
if err != nil {
glog.Errorf("Failed to marshal info: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
w.Write([]byte("Failed to marshal info"))
return
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/server/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ func makeListHandler(defaultNamespace string,
functions = append(functions, function)
}

functionBytes, _ := json.Marshal(functions)
functionBytes, err := json.Marshal(functions)
if err != nil {
glog.Errorf("Failed to marshal functions: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to marshal functions"))
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(functionBytes)

}
}
10 changes: 9 additions & 1 deletion pkg/server/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ import (

"github.com/openfaas/faas-netes/pkg/handlers"
"k8s.io/client-go/kubernetes"
glog "k8s.io/klog"
)

func makeListNamespaceHandler(defaultNamespace string, clientset kubernetes.Interface) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
res := handlers.ListNamespaces(defaultNamespace, clientset)

out, _ := json.Marshal(res)
out, err := json.Marshal(res)
if err != nil {
glog.Errorf("Failed to marshal namespaces: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to marshal namespaces"))
return
}

w.Header().Set("Content-Type", "application/json")

w.WriteHeader(http.StatusOK)
Expand Down
9 changes: 8 additions & 1 deletion pkg/server/replicas.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ func makeReplicaReader(defaultNamespace string, client clientset.Interface, list
Namespace: lookupNamespace,
}

res, _ := json.Marshal(result)
res, err := json.Marshal(result)
if err != nil {
glog.Errorf("Failed to marshal function status: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to marshal function status"))
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(res)
Expand Down

0 comments on commit d00b38d

Please sign in to comment.