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"
)
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
}

View file

@ -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