Skip to content

Commit

Permalink
Handled duplicate code while logging response
Browse files Browse the repository at this point in the history
  • Loading branch information
srkgupta committed Mar 30, 2023
1 parent 64338c8 commit b6ff1d8
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,8 @@ func (p *Plugin) checkAuth(handler http.HandlerFunc) http.HandlerFunc {
func (p *Plugin) handleResponse(fn func(w http.ResponseWriter, r *http.Request) (int, error)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
status, err := fn(w, r)
if status == 0 || status == http.StatusOK {
return
}
if err != nil {
p.API.LogWarn("ERROR: ", "Status", strconv.Itoa(status), "Error", err.Error(), "Path", r.URL.Path, "Method", r.Method, "query", r.URL.Query().Encode())
}

if status != http.StatusOK {
p.API.LogDebug("unexpected plugin response", "Status", strconv.Itoa(status), "Path", r.URL.Path, "Method", r.Method, "query", r.URL.Query().Encode())
}
p.logResponse(r, status, err)
}
}

Expand All @@ -292,8 +284,21 @@ func (p *Plugin) handleResponseWithCallbackInstance(fn func(w http.ResponseWrite
return
}

p.handleResponse(func(w http.ResponseWriter, r *http.Request) (int, error) {
return fn(w, r, callbackInstanceID)
})
status, err := fn(w, r, callbackInstanceID)

p.logResponse(r, status, err)
}
}

func (p *Plugin) logResponse(r *http.Request, status int, err error) {
if status == 0 || status == http.StatusOK {
return
}
if err != nil {
p.API.LogWarn("ERROR: ", "Status", strconv.Itoa(status), "Error", err.Error(), "Path", r.URL.Path, "Method", r.Method, "query", r.URL.Query().Encode())
}

if status != http.StatusOK {
p.API.LogDebug("unexpected plugin response", "Status", strconv.Itoa(status), "Path", r.URL.Path, "Method", r.Method, "query", r.URL.Query().Encode())
}
}

0 comments on commit b6ff1d8

Please sign in to comment.