Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions apiutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,25 @@ func NewErrorResponse(status int, message string) ErrorResponse {
}

func ServeJSON(w http.ResponseWriter, v interface{}) {
serve(w, v, http.StatusOK)
}

func ServeError(w http.ResponseWriter, errRes ErrorResponse) {
serve(w, errRes, errRes.Status)
}

func serve(w http.ResponseWriter, v interface{}, status int) {
content, err := json.MarshalIndent(v, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(content)))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(content)
}

func ServeError(w http.ResponseWriter, errRes ErrorResponse) {
w.WriteHeader(errRes.Status)
ServeJSON(w, errRes)
}

const (
StatusUnprocessableEntity = 422
)
Expand Down