Skip to content

Commit 729dd43

Browse files
fix(check): improve format of errors on multiple checks (#466)
* fix(check): improve format of errors on multiple checks * fix(check): improve log when check is starting
1 parent 81c573a commit 729dd43

File tree

2 files changed

+79
-29
lines changed

2 files changed

+79
-29
lines changed

pkg/check/runner.go

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,39 +91,40 @@ func (c *CheckRunner) Run(ctx context.Context, checks []string) error {
9191
})
9292
}
9393

94-
type errorCheck struct {
95-
check string
96-
err error
97-
}
98-
99-
var errors []errorCheck
94+
checkResults := make([]checkResult, 0, len(validatedChecks))
95+
hasFailures := false
10096

97+
// run checks
10198
for _, check := range validatedChecks {
102-
c.logger.WithField("type", check.typeName).Infof("running check: %s", check.name)
103-
c.logger.Debugf("check options: %+v", check.options)
104-
105-
if err := check.Run(ctx, c.cluster); err != nil {
106-
c.logger.WithField("type", check.typeName).Errorf("check '%s' failed: %v", check.name, err)
107-
errors = append(errors, errorCheck{
108-
check: check.name,
109-
err: fmt.Errorf("check %s failed: %w", check.name, err),
110-
})
99+
c.logger.WithFields(map[string]interface{}{
100+
"type": check.typeName,
101+
"options": fmt.Sprintf("%+v", check.options),
102+
}).Infof("running check: %s", check.name)
103+
104+
err := check.Run(ctx, c.cluster)
105+
if err != nil {
106+
hasFailures = true
107+
c.logger.WithFields(map[string]interface{}{
108+
"type": check.typeName,
109+
"error": err,
110+
}).Errorf("'%s' check failed", check.name)
111111
} else {
112-
c.logger.WithField("type", check.typeName).Infof("%s check completed successfully", check.name)
112+
c.logger.WithField("type", check.typeName).Infof("'%s' check completed successfully", check.name)
113113
}
114+
115+
// append check result
116+
checkResults = append(checkResults, checkResult{
117+
check: check.name,
118+
err: err,
119+
timestamp: time.Now(),
120+
})
114121
}
115122

116-
if len(errors) == 1 {
117-
return errors[0].err
118-
} else if len(errors) > 1 {
119-
var errStrings []string
120-
for _, e := range errors {
121-
errStrings = append(errStrings, fmt.Sprintf("[%s]: {%v}", e.check, e.err))
122-
c.logger.Errorf("%s: %v", e.check, e.err)
123-
}
124-
return fmt.Errorf("multiple checks failed: %s", strings.Join(errStrings, "; "))
123+
if hasFailures {
124+
return formatErrorReport(checkResults)
125125
}
126126

127+
c.logger.WithField("total_checks", len(checkResults)).Info("All checks completed successfully")
127128
return nil
128129
}
129130

@@ -166,3 +167,52 @@ func createChildContext(ctx context.Context, timeout *time.Duration) (context.Co
166167
}
167168
return context.WithCancel(ctx)
168169
}
170+
171+
type checkResult struct {
172+
check string
173+
err error
174+
timestamp time.Time
175+
}
176+
177+
func formatErrorReport(results []checkResult) error {
178+
var failedChecks []string
179+
var failedDetails []string
180+
181+
// if there is only one error, return it directly
182+
if len(results) == 1 {
183+
return results[0].err
184+
}
185+
186+
for _, result := range results {
187+
if result.err != nil {
188+
failedChecks = append(failedChecks, result.check)
189+
failedDetails = append(failedDetails, result.DetailString())
190+
}
191+
}
192+
193+
totalChecks := len(results)
194+
failedCount := len(failedChecks)
195+
196+
return fmt.Errorf("CHECK_FAILED | %d/%d checks failed | Checks: %s | %s",
197+
failedCount,
198+
totalChecks,
199+
strings.Join(failedChecks, ","),
200+
strings.Join(failedDetails, " | "))
201+
}
202+
203+
func (e checkResult) String() string {
204+
if e.err != nil {
205+
return fmt.Sprintf("%s: %v", e.check, e.err)
206+
}
207+
return fmt.Sprintf("%s: success", e.check)
208+
}
209+
210+
func (e checkResult) DetailString() string {
211+
timestamp := e.timestamp.Format("2006-01-02T15:04:05")
212+
if e.err != nil {
213+
return fmt.Sprintf(`{"check":"%s","time":"%s","error":"%v"}`,
214+
e.check, timestamp, e.err)
215+
}
216+
return fmt.Sprintf(`{"check":"%s","time":"%s","status":"success"}`,
217+
e.check, timestamp)
218+
}

pkg/stamper/stamper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (s *Client) Create(ctx context.Context, duration time.Duration, depth uint1
9999
s.log.WithFields(map[string]interface{}{
100100
"duration": duration,
101101
"depth": depth,
102-
}).Infof("creating postage batch on nodes")
102+
}).Info("creating postage batch on nodes")
103103

104104
nodes, err := s.getNodes(ctx)
105105
if err != nil {
@@ -125,7 +125,7 @@ func (s *Client) Dilute(ctx context.Context, usageThreshold float64, dilutionDep
125125
s.log.WithFields(map[string]interface{}{
126126
"usageThreshold": usageThreshold,
127127
"dilutionDepth": dilutionDepth,
128-
}).Infof("diluting postage batch on nodes")
128+
}).Info("diluting postage batch on nodes")
129129

130130
nodes, err := s.getNodes(ctx)
131131
if err != nil {
@@ -148,7 +148,7 @@ func (s *Client) Set(ctx context.Context, ttlThreshold time.Duration, topupTo ti
148148
"topupTo": topupTo,
149149
"usageThreshold": usageThreshold,
150150
"dilutionDepth": dilutionDepth,
151-
}).Infof("setting topup and dilution on postage batch on nodes")
151+
}).Info("setting topup and dilution on postage batch on nodes")
152152

153153
nodes, err := s.getNodes(ctx)
154154
if err != nil {
@@ -174,7 +174,7 @@ func (s *Client) Topup(ctx context.Context, ttlThreshold time.Duration, topupTo
174174
s.log.WithFields(map[string]interface{}{
175175
"ttlThreshold": ttlThreshold,
176176
"topupTo": topupTo,
177-
}).Infof("topup postage batch on nodes")
177+
}).Info("topup postage batch on nodes")
178178

179179
nodes, err := s.getNodes(ctx)
180180
if err != nil {

0 commit comments

Comments
 (0)