From 926d49cc92d69a8fff8b447d477b1ecde84c1a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Garc=C3=ADa=20Crespo?= Date: Thu, 22 Jun 2023 18:18:15 +0000 Subject: [PATCH] Avoid writing errors if conn is hijacked --- internal/api/api.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/api/api.go b/internal/api/api.go index d9775c25..83f03474 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -114,8 +114,16 @@ func errorHandler(logger logr.Logger, msg string) func(context.Context, http.Res if !ok { reqID = "unknown" } - _ = json.NewEncoder(w).Encode(&errorMessage{RequestID: reqID}) - logger.Error(err, "Package service error.", "reqID", reqID) + + // Only write the error if the connection is not hijacked. + var ws bool + if _, err := w.Write(nil); err == http.ErrHijacked { + ws = true + } else { + _ = json.NewEncoder(w).Encode(&errorMessage{RequestID: reqID}) + } + + logger.Error(err, "Service error.", "reqID", reqID, "ws", ws) } }