diff --git a/types/common.go b/types/common.go index 2f1bab73..be1c19e2 100644 --- a/types/common.go +++ b/types/common.go @@ -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("") +} diff --git a/upstream/jobs.go b/upstream/jobs.go index 034d2367..c85f8b87 100644 --- a/upstream/jobs.go +++ b/upstream/jobs.go @@ -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" @@ -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 @@ -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 } @@ -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)) } }