From 917d7571f2962ffd0ca0967557c207e7e6ba6ff5 Mon Sep 17 00:00:00 2001 From: Bastien Riviere Date: Sun, 3 Sep 2023 14:57:43 +0200 Subject: [PATCH] refactor: skip notification via err --- bridge/bridge.go | 16 +++++++++------- bridge/flux.go | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/bridge/bridge.go b/bridge/bridge.go index f9842bc..2fd6204 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -11,6 +11,10 @@ import ( "strings" ) +var ( + errSkipNotification = errors.New("notification skipped") +) + type Handler interface { FormatNotification(r io.Reader) (Notification, error) } @@ -131,16 +135,14 @@ func (b Bridge) ServeHTTP(w http.ResponseWriter, r *http.Request) { not, err := b.h.FormatNotification(r.Body) defer r.Body.Close() - if err != nil { - slog.Error("failed to format notification") - w.WriteHeader(http.StatusBadRequest) + if errors.Is(err, errSkipNotification) { + w.WriteHeader(http.StatusOK) return } - // If notification is empty, that means it should be ignored. - // TODO: maybe return an error instead of empty notification - if not.IsEmpty() { - w.WriteHeader(http.StatusOK) + if err != nil { + slog.Error("failed to format notification") + w.WriteHeader(http.StatusBadRequest) return } diff --git a/bridge/flux.go b/bridge/flux.go index e82f632..357d447 100644 --- a/bridge/flux.go +++ b/bridge/flux.go @@ -62,7 +62,7 @@ func (f FluxHandler) FormatNotification(r io.Reader) (Notification, error) { if not.Reason == "ReconciliationSucceeded" { if ok := f.reconciliations[obj]; !ok { // Filter out spammy ReconciliationSucceeded notification - return Notification{}, nil + return Notification{}, errSkipNotification } // we will print the object so skip it next time it spam