From dca8245ba4bbc342efec083272fcfa0cf27a3ac8 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 20 Mar 2016 11:54:53 +0100 Subject: [PATCH] cmd/syncthing: Return 500 with an error object instead of empty 200 on marshalling failure in REST response --- cmd/syncthing/gui.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 3cd66941f..8e495b3d9 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -149,7 +149,16 @@ func (s *apiService) getListener(guiCfg config.GUIConfiguration) (net.Listener, func sendJSON(w http.ResponseWriter, jsonObject interface{}) { w.Header().Set("Content-Type", "application/json; charset=utf-8") - json.NewEncoder(w).Encode(jsonObject) + // Marshalling might fail, in which case we should return a 500 with the + // actual error. + bs, err := json.Marshal(jsonObject) + if err != nil { + // This Marshal() can't fail though. + bs, _ = json.Marshal(map[string]string{"error": err.Error()}) + http.Error(w, string(bs), http.StatusInternalServerError) + return + } + w.Write(bs) } func (s *apiService) Serve() {