diff --git a/http/http.go b/http/http.go index aa94e10..9fe894a 100644 --- a/http/http.go +++ b/http/http.go @@ -28,35 +28,34 @@ type Server struct { func makeHandler(handler ResponseHandler) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + var msg string + start := time.Now() httpCode, err := handler(w, r, p) duration := time.Since(start) if err != nil { - stats.RecordEvent(stats.ErroredRequest) - http.Error(w, err.Error(), httpCode) + msg = err.Error() + } else if httpCode != http.StatusOK { + msg = http.StatusText(httpCode) + } - glog.Errorf( - "Failed (%v:%s) %s with %s in %s", - httpCode, - http.StatusText(httpCode), - r.URL.Path, - err.Error(), - duration, - ) - } else if glog.V(2) { + if len(msg) > 0 { + http.Error(w, msg, httpCode) + stats.RecordEvent(stats.ErroredRequest) + } + + if len(msg) > 0 || glog.V(2) { reqString := r.URL.Path if glog.V(3) { - reqString = r.URL.RequestURI() + " for " + r.RemoteAddr + reqString = r.URL.RequestURI() + " " + r.RemoteAddr } - glog.Infof( - "Completed (%v:%s) %s in %v", - httpCode, - http.StatusText(httpCode), - reqString, - duration, - ) + if len(msg) > 0 { + glog.Errorf("[%d: %9s] %s (%s)", httpCode, duration, reqString, msg) + } else { + glog.Infof("[%d: %9s] %s", httpCode, duration, reqString) + } } stats.RecordEvent(stats.HandledRequest) diff --git a/http/routes.go b/http/routes.go index 9780f1a..1aa8ce1 100644 --- a/http/routes.go +++ b/http/routes.go @@ -21,12 +21,11 @@ const jsonContentType = "application/json; charset=UTF-8" func handleError(err error) (int, error) { if err == nil { return http.StatusOK, nil + } else if _, ok := err.(models.NotFoundError); ok { + stats.RecordEvent(stats.ClientError) + return http.StatusNotFound, nil } else if _, ok := err.(models.ClientError); ok { stats.RecordEvent(stats.ClientError) - - if _, ok := err.(models.NotFoundError); ok { - return http.StatusNotFound, nil - } return http.StatusBadRequest, nil } return http.StatusInternalServerError, err