kinda working
This commit is contained in:
31
json.go
Normal file
31
json.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func respondWithError(w http.ResponseWriter, code int, msg string) {
|
||||
if code > 499 {
|
||||
log.Printf("Responding with 5XX error: %s", msg)
|
||||
}
|
||||
type errorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
respondWithJSON(w, code, errorResponse{
|
||||
Error: msg,
|
||||
})
|
||||
}
|
||||
|
||||
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
dat, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling JSON: %s", err)
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(code)
|
||||
w.Write(dat)
|
||||
}
|
||||
Reference in New Issue
Block a user