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
This commit is contained in:
parent
bdbcf826eb
commit
9a7f26995f
1 changed files with 6 additions and 13 deletions
|
@ -1,23 +1,16 @@
|
||||||
package other
|
package other
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HttpErr(w http.ResponseWriter, errId int, message string, code int) error {
|
// Return an error over an http connection.
|
||||||
type errStruct struct {
|
// The error will have the given return code `code`
|
||||||
Id int `json:"id"`
|
// and a json encoded body with the field "id" set to `errId`
|
||||||
Message string `json:"message"`
|
// and a field "message" set to the `message`
|
||||||
}
|
func HttpErr(w http.ResponseWriter, errId int, message string, code int) {
|
||||||
|
|
||||||
data, err := json.Marshal(errStruct{errId, message})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.WriteHeader(code)
|
w.WriteHeader(code)
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
fmt.Fprint(w, data)
|
fmt.Fprintf(w, "{\"id\": %d, \"message\": \"%s\"}", errId, message)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue