Skip to content

Commit a1e1b5d

Browse files
committed
Refactor respond JSON
1 parent a2ec25a commit a1e1b5d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

log.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
)
88

99
func respond(w http.ResponseWriter, r *http.Request, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string, success bool, desc string) {
10-
response, _ := json.Marshal(result)
1110
w.Header().Set("Content-Type", "application/json")
1211
w.WriteHeader(code)
13-
w.Write(response)
12+
err := json.NewEncoder(w).Encode(result)
13+
if err != nil {
14+
http.Error(w, err.Error(), http.StatusInternalServerError)
15+
}
1416
if writeLog != nil {
1517
writeLog(r.Context(), resource, action, success, desc)
1618
}

0 commit comments

Comments
 (0)