Skip to content

Commit

Permalink
http: log file of HTTPError origin
Browse files Browse the repository at this point in the history
  • Loading branch information
dissoupov committed Oct 31, 2021
1 parent 61b8af0 commit f84cb63
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions xhttp/marshal/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
goErrors "errors"
"io"
"net/http"
"runtime"
"strings"

"github.com/go-phorce/dolly/xhttp/header"
Expand Down Expand Up @@ -49,15 +50,15 @@ func WriteJSON(w http.ResponseWriter, r *http.Request, bodies ...interface{}) {
case WriteHTTPResponse:
// errors.Error impls WriteHTTPResponse, so will take this path and do its thing
bv.WriteHTTPResponse(w, r)
tryLogHTTPError(bv, r)
httpError(bv, r)
return

case error:
var resp WriteHTTPResponse

if goErrors.As(bv, &resp) {
resp.WriteHTTPResponse(w, r)
tryLogHTTPError(bv, r)
httpError(bv, r)
return
}

Expand All @@ -83,17 +84,21 @@ func WriteJSON(w http.ResponseWriter, r *http.Request, bodies ...interface{}) {
}
}

func tryLogHTTPError(bv interface{}, r *http.Request) {
func httpError(bv interface{}, r *http.Request) {
// notice that we're using 2, so it will actually log where
// the error happened, 0 = this function, we don't want that.
_, fn, line, _ := runtime.Caller(2)

if e, ok := bv.(*httperror.Error); ok {
if e.HTTPStatus >= 500 {
logger.Errorf("INTERNAL_ERROR=%s:%d:%s:%s",
r.URL.Path, e.HTTPStatus, e.Code, e.Message)
logger.Errorf("INTERNAL_ERROR=%s:%d:%s:%s:%s:%d",
r.URL.Path, e.HTTPStatus, e.Code, e.Message, fn, line)
if e.Cause != nil {
logger.Errorf("err=[%+v]", e.Cause)
}
} else {
logger.Warningf("API_ERROR=%s:%d:%s:%s",
r.URL.Path, e.HTTPStatus, e.Code, e.Message)
logger.Warningf("API_ERROR=%s:%d:%s:%s:%s:%d",
r.URL.Path, e.HTTPStatus, e.Code, e.Message, fn, line)
if e.Cause != nil {
logger.Warningf("err=[%+v]", e.Cause)
}
Expand Down

0 comments on commit f84cb63

Please sign in to comment.