Skip to content

Commit

Permalink
Export ChunkError fields and add ErrorsFor convenience method (truffl…
Browse files Browse the repository at this point in the history
  • Loading branch information
mcastorina authored Oct 19, 2023
1 parent 8058006 commit 7583447
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pkg/sources/job_progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ func (f Fatal) Unwrap() error { return f.error }
// ChunkError is a custom error type for errors encountered during chunking of
// a specific unit.
type ChunkError struct {
unit SourceUnit
err error
Unit SourceUnit
Err error
}

func (f ChunkError) Error() string {
return fmt.Sprintf("error chunking unit %q: %s", f.unit.SourceUnitID(), f.err.Error())
return fmt.Sprintf("error chunking unit %q: %s", f.Unit.SourceUnitID(), f.Err.Error())
}
func (f ChunkError) Unwrap() error { return f.err }
func (f ChunkError) Unwrap() error { return f.Err }

// JobProgress aggregates information about a run of a Source.
type JobProgress struct {
Expand Down Expand Up @@ -351,3 +351,17 @@ func (m JobProgressMetrics) ElapsedTime() time.Duration {
}
return m.EndTime.Sub(m.StartTime)
}

// ErrorsFor returns all the errors for the given SourceUnit. If there are no
// errors, including the case that the unit has not been encountered, nil will
// be returned.
func (m JobProgressMetrics) ErrorsFor(unit SourceUnit) []error {
var errs []error
for _, err := range m.Errors {
var chunkErr ChunkError
if errors.As(err, &chunkErr) && chunkErr.Unit == unit {
errs = append(errs, err)
}
}
return errs
}
23 changes: 23 additions & 0 deletions pkg/sources/job_progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,26 @@ func TestJobProgressElapsedTime(t *testing.T) {
metrics.EndTime = metrics.StartTime.Add(1 * time.Hour)
assert.Equal(t, metrics.ElapsedTime(), 1*time.Hour)
}

func TestJobProgressErrorsFor(t *testing.T) {
metrics := JobProgressMetrics{
Errors: []error{
Fatal{ChunkError{
Unit: CommonSourceUnit{ID: "foo"},
Err: fmt.Errorf("foo error"),
}},
ChunkError{
Unit: CommonSourceUnit{ID: "foo"},
Err: fmt.Errorf("foo again error"),
},
ChunkError{
Unit: CommonSourceUnit{ID: "bar"},
Err: fmt.Errorf("bar error"),
},
fmt.Errorf("hi there"),
},
}
assert.Equal(t, 2, len(metrics.ErrorsFor(CommonSourceUnit{ID: "foo"})))
assert.Equal(t, 1, len(metrics.ErrorsFor(CommonSourceUnit{ID: "bar"})))
assert.Equal(t, 0, len(metrics.ErrorsFor(CommonSourceUnit{ID: "baz"})))
}

0 comments on commit 7583447

Please sign in to comment.