From 9a7f26995fb1d6c4e490353a7377e61b93b80fce Mon Sep 17 00:00:00 2001 From: mstar Date: Sun, 13 Oct 2024 13:27:16 +0200 Subject: [PATCH] Update HttpErr It now no longer has to go through json encoding and uses a hardcoded string instead. Also clears the one location where an error could occur --- other/httpErr.go | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/other/httpErr.go b/other/httpErr.go index ce8b2b1..d36574d 100644 --- a/other/httpErr.go +++ b/other/httpErr.go @@ -1,23 +1,16 @@ package other import ( - "encoding/json" "fmt" "net/http" ) -func HttpErr(w http.ResponseWriter, errId int, message string, code int) error { - type errStruct struct { - Id int `json:"id"` - Message string `json:"message"` - } - - data, err := json.Marshal(errStruct{errId, message}) - if err != nil { - return err - } +// Return an error over an http connection. +// The error will have the given return code `code` +// and a json encoded body with the field "id" set to `errId` +// and a field "message" set to the `message` +func HttpErr(w http.ResponseWriter, errId int, message string, code int) { w.WriteHeader(code) w.Header().Add("Content-Type", "application/json") - fmt.Fprint(w, data) - return nil + fmt.Fprintf(w, "{\"id\": %d, \"message\": \"%s\"}", errId, message) }