Add basic http error formatter

This commit is contained in:
Melody Becker 2024-09-28 12:37:06 +02:00
parent f6878107d8
commit bd60c842e9
No known key found for this signature in database

23
other/httpErr.go Normal file
View 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
}