From dbcf08b197b6b08352597588611e489f8188f643 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sat, 15 Jan 2022 14:43:29 +0100 Subject: [PATCH] Handle json encoding errors --- main.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 490b7ee..0b60c73 100644 --- a/main.go +++ b/main.go @@ -18,12 +18,18 @@ type Metadata struct { func PipelineHandler(pipeline pipelines.IPipeline, w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(pipeline) + err := json.NewEncoder(w).Encode(pipeline) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + } } func IndexHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(Metadata{Name, Version}) + err := json.NewEncoder(w).Encode(Metadata{Name, Version}) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + } } func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline) { @@ -42,5 +48,8 @@ func main() { RegisterPipelineRoutes(r, pipes) - http.ListenAndServe(":8000", r) + err := http.ListenAndServe(":8000", r) + if err != nil { + panic(err) + } }