Merge pull request #122925 from tkashem/timeout-refactor-handle-error
apiserver: refactor handleError in endpoints/filters
This commit is contained in:
		@@ -58,7 +58,7 @@ func withRequestDeadline(handler http.Handler, sink audit.Sink, policy audit.Pol
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		requestInfo, ok := request.RequestInfoFrom(ctx)
 | 
							requestInfo, ok := request.RequestInfoFrom(ctx)
 | 
				
			||||||
		if !ok {
 | 
							if !ok {
 | 
				
			||||||
			handleError(w, req, http.StatusInternalServerError, fmt.Errorf("no RequestInfo found in context, handler chain must be wrong"))
 | 
								handleError(w, req, http.StatusInternalServerError, nil, "no RequestInfo found in context, handler chain must be wrong")
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if longRunning(req, requestInfo) {
 | 
							if longRunning(req, requestInfo) {
 | 
				
			||||||
@@ -166,8 +166,12 @@ func parseTimeout(req *http.Request) (time.Duration, bool, error) {
 | 
				
			|||||||
	return timeout, true, nil
 | 
						return timeout, true, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func handleError(w http.ResponseWriter, r *http.Request, code int, err error) {
 | 
					// handleError does the following:
 | 
				
			||||||
	errorMsg := fmt.Sprintf("Error - %s: %#v", err.Error(), r.RequestURI)
 | 
					// a) it writes the specified error code, and msg to the ResponseWriter
 | 
				
			||||||
	http.Error(w, errorMsg, code)
 | 
					// object, it does not print the given innerErr into the ResponseWriter object.
 | 
				
			||||||
	klog.Errorf(errorMsg)
 | 
					// b) additionally, it prints the given msg, and innerErr to the log with other
 | 
				
			||||||
 | 
					// request scoped data that helps identify the given request.
 | 
				
			||||||
 | 
					func handleError(w http.ResponseWriter, r *http.Request, code int, innerErr error, msg string) {
 | 
				
			||||||
 | 
						http.Error(w, msg, code)
 | 
				
			||||||
 | 
						klog.ErrorSDepth(1, innerErr, msg, "method", r.Method, "URI", r.RequestURI, "auditID", audit.GetAuditIDTruncated(r.Context()))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,7 +18,6 @@ package filters
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -39,7 +38,7 @@ func WithLatencyTrackers(handler http.Handler) http.Handler {
 | 
				
			|||||||
		ctx := req.Context()
 | 
							ctx := req.Context()
 | 
				
			||||||
		requestInfo, ok := request.RequestInfoFrom(ctx)
 | 
							requestInfo, ok := request.RequestInfoFrom(ctx)
 | 
				
			||||||
		if !ok {
 | 
							if !ok {
 | 
				
			||||||
			handleError(w, req, http.StatusInternalServerError, fmt.Errorf("no RequestInfo found in context, handler chain must be wrong"))
 | 
								handleError(w, req, http.StatusInternalServerError, nil, "no RequestInfo found in context, handler chain must be wrong")
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user