knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/network/error_handler.go (about) 1 /* 2 Copyright 2019 The Knative Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package network 18 19 import ( 20 "context" 21 "errors" 22 "net/http" 23 "os" 24 25 "go.uber.org/zap" 26 ) 27 28 // ErrorHandler sets up a handler suitable for use with the ErrorHandler field on 29 // httputil's reverse proxy. 30 // 31 // Deprecated: Use handler.Error instead. 32 func ErrorHandler(logger *zap.SugaredLogger) func(http.ResponseWriter, *http.Request, error) { 33 return func(w http.ResponseWriter, req *http.Request, err error) { 34 if errors.Is(err, context.Canceled) { 35 logger.Debugw("request context canceled", zap.Error(err)) 36 return 37 } 38 ss := readSockStat(logger) 39 logger.Errorw("error reverse proxying request; sockstat: "+ss, zap.Error(err)) 40 http.Error(w, err.Error(), http.StatusBadGateway) 41 } 42 } 43 44 func readSockStat(logger *zap.SugaredLogger) string { 45 b, err := os.ReadFile("/proc/net/sockstat") 46 if err != nil { 47 logger.Errorw("Unable to read sockstat", zap.Error(err)) 48 return "" 49 } 50 return string(b) 51 }