Add basic http error formatter
This commit is contained in:
parent
f6878107d8
commit
bd60c842e9
1 changed files with 23 additions and 0 deletions
23
other/httpErr.go
Normal file
23
other/httpErr.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
w.WriteHeader(code)
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
fmt.Fprint(w, data)
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in a new issue