refactor: skip notification via err

This commit is contained in:
Bastien Riviere 2023-09-03 14:57:43 +02:00
parent 9f303dcb89
commit 917d7571f2
Signed by: babariviere
GPG key ID: 4E5F0839249F162E
2 changed files with 10 additions and 8 deletions

View file

@ -11,6 +11,10 @@ import (
"strings" "strings"
) )
var (
errSkipNotification = errors.New("notification skipped")
)
type Handler interface { type Handler interface {
FormatNotification(r io.Reader) (Notification, error) 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) not, err := b.h.FormatNotification(r.Body)
defer r.Body.Close() defer r.Body.Close()
if err != nil { if errors.Is(err, errSkipNotification) {
slog.Error("failed to format notification") w.WriteHeader(http.StatusOK)
w.WriteHeader(http.StatusBadRequest)
return return
} }
// If notification is empty, that means it should be ignored. if err != nil {
// TODO: maybe return an error instead of empty notification slog.Error("failed to format notification")
if not.IsEmpty() { w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusOK)
return return
} }

View file

@ -62,7 +62,7 @@ func (f FluxHandler) FormatNotification(r io.Reader) (Notification, error) {
if not.Reason == "ReconciliationSucceeded" { if not.Reason == "ReconciliationSucceeded" {
if ok := f.reconciliations[obj]; !ok { if ok := f.reconciliations[obj]; !ok {
// Filter out spammy ReconciliationSucceeded notification // Filter out spammy ReconciliationSucceeded notification
return Notification{}, nil return Notification{}, errSkipNotification
} }
// we will print the object so skip it next time it spam // we will print the object so skip it next time it spam