21 lines
402 B
Go
21 lines
402 B
Go
package httpx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type ErrorResponse struct {
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
func WriteJSON(w http.ResponseWriter, code int, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(code)
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
}
|
|
|
|
func WriteError(w http.ResponseWriter, code int, err error) {
|
|
WriteJSON(w, code, ErrorResponse{Error: err.Error()})
|
|
}
|