chore: disable error interceptor on websocket
This commit is contained in:
@@ -43,14 +43,17 @@ func (rec *responseRecorder) Write(data []byte) (int, error) {
|
|||||||
// ErrorInterceptor Middleware intercepts backend errors
|
// ErrorInterceptor Middleware intercepts backend errors
|
||||||
func (intercept InterceptErrors) ErrorInterceptor(next http.Handler) http.Handler {
|
func (intercept InterceptErrors) ErrorInterceptor(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Check if the connection is a WebSocket
|
||||||
|
if isWebSocketRequest(r) {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
rec := newResponseRecorder(w)
|
rec := newResponseRecorder(w)
|
||||||
next.ServeHTTP(rec, r)
|
next.ServeHTTP(rec, r)
|
||||||
w.Header().Set("Proxied-By", "Goma Gateway")
|
|
||||||
w.Header().Del("Server") //Delete server name
|
|
||||||
if canIntercept(rec.statusCode, intercept.Errors) {
|
if canIntercept(rec.statusCode, intercept.Errors) {
|
||||||
logger.Debug("An error occurred in the backend, %d", rec.statusCode)
|
logger.Error("Request to %s resulted in error with status code %d\n", r.URL.Path, rec.statusCode)
|
||||||
logger.Error("Backend error: %d", rec.statusCode)
|
|
||||||
RespondWithError(w, rec.statusCode, http.StatusText(rec.statusCode))
|
RespondWithError(w, rec.statusCode, http.StatusText(rec.statusCode))
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
// No error: write buffered response to client
|
// No error: write buffered response to client
|
||||||
w.WriteHeader(rec.statusCode)
|
w.WriteHeader(rec.statusCode)
|
||||||
@@ -63,6 +66,9 @@ func (intercept InterceptErrors) ErrorInterceptor(next http.Handler) http.Handle
|
|||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
func isWebSocketRequest(r *http.Request) bool {
|
||||||
|
return r.Header.Get("Upgrade") == "websocket" && r.Header.Get("Connection") == "Upgrade"
|
||||||
|
}
|
||||||
func canIntercept(code int, errors []int) bool {
|
func canIntercept(code int, errors []int) bool {
|
||||||
return slices.Contains(errors, code)
|
return slices.Contains(errors, code)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user