mirror of
https://github.com/kevin-DL/football_info_api.git
synced 2026-01-11 10:04:28 +00:00
35 lines
642 B
Go
35 lines
642 B
Go
package helpers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func RespondWithError(w http.ResponseWriter, code int, msg string) {
|
|
if code > 499 {
|
|
log.Println("Responding with 5XX error: ", msg)
|
|
}
|
|
|
|
type errResponse struct {
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
RespondWithJson(w, code, errResponse{
|
|
Error: msg,
|
|
})
|
|
}
|
|
|
|
func RespondWithJson(w http.ResponseWriter, code int, payload interface{}) {
|
|
data, err := json.Marshal(payload)
|
|
if err != nil {
|
|
log.Printf("Failed to marshall JSON response: %v", payload)
|
|
w.WriteHeader(500)
|
|
return
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
w.WriteHeader(code)
|
|
w.Write(data)
|
|
}
|