Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RHINENG-9452: refactor error handling #1465

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion base/errors.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
package base

import (
"errors"
stdErrors "errors"

"github.com/pkg/errors"
)

var (
ErrDatabase = errors.New("database error")
ErrKafka = errors.New("kafka error")
ErrBadRequest = errors.New("bad request")
ErrNotFound = errors.New("not found")
ErrFatal = errors.New("fatal error restarting pod")
)

func WrapFatalError(err error, message string) error {
return wrapErrors(err, message)
}

func WrapFatalDBError(err error, message string) error {
return wrapErrors(err, message, ErrFatal, ErrDatabase)
}

func WrapFatalKafkaError(err error, message string) error {
return wrapErrors(err, message, ErrFatal, ErrKafka)
}

func wrapErrors(err error, message string, errs ...error) error {
if err == nil {
return nil
}
errsJoined := stdErrors.Join(errs...)
err = stdErrors.Join(errsJoined, err)
return errors.Wrap(err, message)
}
8 changes: 6 additions & 2 deletions base/mqueue/mqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"app/base"
"app/base/utils"
"context"
"errors"
format "fmt"
"io"
"strings"
Expand Down Expand Up @@ -55,13 +56,16 @@ func MakeRetryingHandler(handler MessageHandler) MessageHandler {
backoffState, cancel := policy.Start(base.Context)
defer cancel()
for backoff.Continue(backoffState) {
if err = handler(message); err == nil {
if err = handler(message); err == nil || !errors.Is(err, base.ErrFatal) {
return nil
}
utils.LogError("err", err.Error(), "attempt", attempt, "Try failed")
attempt++
}
return err
if err != nil && errors.Is(err, base.ErrFatal) {
return err
}
return nil
}
}

Expand Down
6 changes: 3 additions & 3 deletions evaluator/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func evaluateInDatabase(ctx context.Context, event *mqueue.PlatformEvent, invent

thirdParty, err := analyzeRepos(system)
if err != nil {
return nil, nil, errors.Wrap(err, "Repo analysis failed")
return nil, nil, errors.Wrap(err, "repo analysis failed")
}
system.ThirdParty = thirdParty // to set "system_platform.third_party" column

Expand Down Expand Up @@ -414,7 +414,7 @@ func tryGetSystem(accountID int, inventoryID string,
system, err := loadSystemData(accountID, inventoryID)
if err != nil {
evaluationCnt.WithLabelValues("error-db-read-inventory-data").Inc()
return nil, errors.Wrap(err, "error loading system from DB")
return nil, base.WrapFatalDBError(err, "error loading system from DB")
}
if system.ID == 0 {
evaluationCnt.WithLabelValues("error-db-read-inventory-data").Inc()
Expand Down Expand Up @@ -496,7 +496,7 @@ func evaluateAndStore(system *models.SystemPlatform,
err = commitWithObserve(tx)
if err != nil {
evaluationCnt.WithLabelValues("error-database-commit").Inc()
return errors.New("database commit failed")
return base.WrapFatalDBError(err, "database commit failed")
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion evaluator/evaluate_baseline.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package evaluator

import (
"app/base"
"app/base/database"
"app/base/models"
"app/base/vmaas"
Expand All @@ -24,7 +25,7 @@ func limitVmaasToBaseline(system *models.SystemPlatform, vmaasData *vmaas.Update
Where("public_date >= ?", baselineConfig.ToTime.Truncate(24*time.Hour)).
Pluck("name", &filterOutNames).Error
if err != nil {
return err
return base.WrapFatalDBError(err, "load reported advisories")
}

// create map of advisories we need to filter out
Expand Down
5 changes: 3 additions & 2 deletions evaluator/remediations.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func publishRemediationsState(system *models.SystemPlatform, response *vmaas.Upd
state := createRemediationsStateMsg(system.InventoryID, response)
msg, err := mqueue.MessageFromJSON(system.InventoryID, state)
if err != nil {
return errors.Wrap(err, "Formatting message")
return errors.Wrap(err, "formatting message")
}
return remediationsPublisher.WriteMessages(base.Context, msg)
err = remediationsPublisher.WriteMessages(base.Context, msg)
return base.WrapFatalKafkaError(err, "write message")
}
Loading
Loading