Skip to content

Commit

Permalink
fix: reconcile error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Sep 22, 2024
1 parent 9848b00 commit 9f6d9f5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 13 additions & 0 deletions types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,16 @@ func (items Items) Where(query *gorm.DB, col string) *gorm.DB {

return query
}

// ErrorString wraps the error to implement custom JSON marshaling
type ErrorString struct {
Err error
}

// Convert the error to its string representation if non-nil, else return an empty string.
func (e ErrorString) MarshalJSON() ([]byte, error) {
if e.Err != nil {
return json.Marshal(e.Err.Error())
}
return json.Marshal("")
}
15 changes: 8 additions & 7 deletions upstream/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/flanksource/duty/context"
dutil "github.com/flanksource/duty/db"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
"github.com/samber/lo"
"github.com/sethvargo/go-retry"
"gorm.io/gorm"
Expand Down Expand Up @@ -47,10 +48,10 @@ var (
)

type ReconcileTableSummary struct {
Success int `json:"success,omitempty"`
FKeyError int `json:"foreign_error,omitempty"`
Skipped bool `json:"skipped,omitempty"`
Error error `json:"error,omitempty"`
Success int `json:"success,omitempty"`
FKeyError int `json:"foreign_error,omitempty"`
Skipped bool `json:"skipped,omitempty"`
Error types.ErrorString `json:"error,omitempty"`
}

type ReconcileSummary map[string]ReconcileTableSummary
Expand All @@ -72,7 +73,7 @@ func (t ReconcileSummary) DidReconcile(tables []string) bool {
return false // this table hasn't been reconciled yet
}

reconciled := !summary.Skipped && summary.Error == nil && summary.FKeyError == 0
reconciled := !summary.Skipped && summary.Error.Err == nil && summary.FKeyError == 0
if !reconciled {
return false // table didn't reconcile successfully
}
Expand Down Expand Up @@ -114,14 +115,14 @@ func (t *ReconcileSummary) AddStat(table string, success, failed int, err error)
v := (*t)[table]
v.Success = success
v.FKeyError = failed
v.Error = err
v.Error = types.ErrorString{Err: err}
(*t)[table] = v
}

func (t ReconcileSummary) Error() error {
var allErrors []string
for table, summary := range t {
if summary.Error != nil {
if summary.Error.Err != nil {
allErrors = append(allErrors, fmt.Sprintf("%s: %s; ", table, summary.Error))
}
}
Expand Down

0 comments on commit 9f6d9f5

Please sign in to comment.