all repos — piggy @ 46e32843c33c2a0a8c7cfb71a71b4456234057b0

Dead simple finance manager in Go, HTML and JS.

src/api/utils.go (view raw)

 1package api
 2
 3import (
 4	"encoding/json"
 5	"log"
 6	"net/http"
 7)
 8
 9type APIError struct {
10	Error string `json:"error"`
11}
12
13func jsonResponse(w http.ResponseWriter, v any) {
14	w.Header().Add("Content-Type", "application/json")
15	err := json.NewEncoder(w).Encode(v)
16	if err != nil {
17		log.Println("could not encode JSON response: " + err.Error())
18	}
19}
20
21func new500Error(w http.ResponseWriter, err error) {
22	http.Error(w, err.Error(), http.StatusInternalServerError)
23}
24
25func new400Error(w http.ResponseWriter, err error) {
26	http.Error(w, err.Error(), http.StatusBadRequest)
27}