morz-infoboard/server/backend/internal/httpapi/errors.go

31 lines
578 B
Go

package httpapi
import (
"encoding/json"
"net/http"
)
type errorEnvelope struct {
Error apiError `json:"error"`
}
type apiError struct {
Code string `json:"code"`
Message string `json:"message"`
Details any `json:"details"`
}
func writeError(w http.ResponseWriter, status int, code, message string, details any) {
writeJSON(w, status, errorEnvelope{
Error: apiError{
Code: code,
Message: message,
Details: details,
},
})
}
func decodeJSON(r *http.Request, dest any) error {
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(dest)
}