Beispiel #1
0
func (s *Server) handler() http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) {
		tid := uuid.NewV4().String()
		cLog := s.log.WithFields(logrus.Fields{"tid": tid})
		cLog.WithFields(logrus.Fields{"method": r.Method, "uri": helpers.SanitizeURL(r.URL)}).Info("request started")
		r = keys.SetLog(r, cLog)
		defer func() {
			cLog.Info("request ended")
			// Catch panic and return 500 with corresponding tid for debugging
			var err error
			r := recover()
			if r != nil {
				switch t := r.(type) {
				case string:
					err = errors.New(t)
				case error:
					err = t
				default:
					err = errors.New(fmt.Sprintln(r))
				}
				trace := make([]byte, 2048)
				count := runtime.Stack(trace, true)
				cLog.Error(fmt.Sprintf("recover from panic: %s\nstack of %d bytes: %s\n", err.Error(), count, trace))
				w.WriteHeader(http.StatusInternalServerError)
				w.Write([]byte(tid))
				return
			}

		}()
		s.router.ServeHTTP(w, r)
	}
	return http.HandlerFunc(handlerFunc)
}
Beispiel #2
0
func (r *ErrorResponse) Error() string {
	return fmt.Sprintf("%v %s: %d (%s)",
		r.Response.Request.Method, helpers.SanitizeURL(r.Response.Request.URL),
		r.Response.StatusCode, r.Err.Error())
}
Beispiel #3
0
func (r *Response) String() string {
	return fmt.Sprintf("%v %v: %d",
		r.Response.Request.Method, helpers.SanitizeURL(r.Response.Request.URL),
		r.Response.StatusCode)

}