Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log json.Marshal errors #682

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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