mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-12 16:26:37 +00:00
cmd/syncthing: Return 500 with an error object instead of empty 200 on marshalling failure in REST response
This commit is contained in:
parent
aba2cc4db2
commit
9dec6f1324
@ -163,7 +163,16 @@ func (s *apiService) getListener(guiCfg config.GUIConfiguration) (net.Listener,
|
|||||||
|
|
||||||
func sendJSON(w http.ResponseWriter, jsonObject interface{}) {
|
func sendJSON(w http.ResponseWriter, jsonObject interface{}) {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
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() {
|
func (s *apiService) Serve() {
|
||||||
|
Loading…
Reference in New Issue
Block a user