From ce6d3c511aac03635448856c1baceed491893f1c Mon Sep 17 00:00:00 2001 From: Lars Karlitski Date: Sun, 5 Apr 2020 08:58:33 +0200 Subject: [PATCH] jobqueue/api: don't panic when error cannot be returned Don't panic when the error message cannot be written to the connection. Ignore it, because there's nothing we can do in this case. The standard library has the same behavior. --- internal/jobqueue/api.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/jobqueue/api.go b/internal/jobqueue/api.go index 0bd4f7db1..9d8dc55f7 100644 --- a/internal/jobqueue/api.go +++ b/internal/jobqueue/api.go @@ -2,6 +2,7 @@ package jobqueue import ( "encoding/json" + "fmt" "log" "net" "net/http" @@ -66,21 +67,19 @@ func notFoundHandler(writer http.ResponseWriter, request *http.Request) { writer.WriteHeader(http.StatusNotFound) } -func statusResponseError(writer http.ResponseWriter, code int, errors ...string) { +func statusResponseError(writer http.ResponseWriter, code int, message string) { writer.WriteHeader(code) - for _, err := range errors { - _, e := writer.Write([]byte(err)) - if e != nil { - panic(e) - } + if message != "" { + // ignore error, because we cannot do anything useful with it + fmt.Fprint(writer, message) } } func (api *API) addJobHandler(writer http.ResponseWriter, request *http.Request, _ httprouter.Params) { contentType := request.Header["Content-Type"] if len(contentType) != 1 || contentType[0] != "application/json" { - statusResponseError(writer, http.StatusUnsupportedMediaType) + statusResponseError(writer, http.StatusUnsupportedMediaType, "") return } @@ -106,7 +105,7 @@ func (api *API) addJobHandler(writer http.ResponseWriter, request *http.Request, func (api *API) updateJobHandler(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { contentType := request.Header["Content-Type"] if len(contentType) != 1 || contentType[0] != "application/json" { - statusResponseError(writer, http.StatusUnsupportedMediaType) + statusResponseError(writer, http.StatusUnsupportedMediaType, "") return }