goutils/other/httpErr.go

24 lines
431 B
Go
Raw Normal View History

2024-09-28 10:37:06 +00:00
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
}