Skip to content

Commit 8b3e522

Browse files
authored
feat(schema): Extract PatternMatchError typed error from FilterDfs (#2458)
Replace plain `fmt.Errorf` calls with a structured `PatternMatchError` type so callers can use `errors.As`/`errors.Is` to extract/validate the invalid pattern and which config key (`tables` or `skip_tables`) caused the mismatch.
1 parent f891f73 commit 8b3e522

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

schema/errors.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ const (
1717
cannotSetIndex = "cannot set index %d"
1818
)
1919

20+
// PatternMatchError is returned by Tables.FilterDfs when a tables or skip_tables
21+
// pattern does not match any known table.
22+
type PatternMatchError struct {
23+
Key string // "tables" or "skip_tables"
24+
Pattern string // the glob pattern that had no matches
25+
}
26+
27+
func (e *PatternMatchError) Error() string {
28+
return fmt.Sprintf("%s include a pattern %s with no matches", e.Key, e.Pattern)
29+
}
30+
2031
type ValidationError struct {
2132
Err error
2233
Msg string

schema/table.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func (tt Tables) FilterDfs(tables, skipTables []string, skipDependentTables bool
395395
}
396396
}
397397
if !matched {
398-
return nil, fmt.Errorf("tables include a pattern %s with no matches", includePattern)
398+
return nil, &PatternMatchError{Key: "tables", Pattern: includePattern}
399399
}
400400
}
401401
for _, excludePattern := range skipTables {
@@ -407,7 +407,7 @@ func (tt Tables) FilterDfs(tables, skipTables []string, skipDependentTables bool
407407
}
408408
}
409409
if !matched {
410-
return nil, fmt.Errorf("skip_tables include a pattern %s with no matches", excludePattern)
410+
return nil, &PatternMatchError{Key: "skip_tables", Pattern: excludePattern}
411411
}
412412
}
413413
include := func(t *Table) bool {

schema/table_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package schema
22

33
import (
4+
"errors"
45
"testing"
56

67
"github.com/apache/arrow-go/v18/arrow"
@@ -444,6 +445,51 @@ func TestTablesFilterDFS(t *testing.T) {
444445
}
445446
}
446447

448+
func TestFilterDfsPatternMatchError(t *testing.T) {
449+
tables := Tables{
450+
{Name: "aws_s3_buckets"},
451+
{Name: "aws_ec2_instances"},
452+
}
453+
454+
tests := []struct {
455+
name string
456+
tables []string
457+
skipTables []string
458+
wantKey string
459+
wantPattern string
460+
wantMessage string
461+
}{
462+
{
463+
name: "unmatched tables pattern",
464+
tables: []string{"aws_no_such_table"},
465+
skipTables: []string{},
466+
wantKey: "tables",
467+
wantPattern: "aws_no_such_table",
468+
wantMessage: "tables include a pattern aws_no_such_table with no matches",
469+
},
470+
{
471+
name: "unmatched skip_tables pattern",
472+
tables: []string{"*"},
473+
skipTables: []string{"gcp_*"},
474+
wantKey: "skip_tables",
475+
wantPattern: "gcp_*",
476+
wantMessage: "skip_tables include a pattern gcp_* with no matches",
477+
},
478+
}
479+
for _, tt := range tests {
480+
t.Run(tt.name, func(t *testing.T) {
481+
_, err := tables.FilterDfs(tt.tables, tt.skipTables, false)
482+
require.Error(t, err)
483+
484+
var patErr *PatternMatchError
485+
require.True(t, errors.As(err, &patErr), "expected errors.As to match *PatternMatchError, got %T", err)
486+
require.Equal(t, tt.wantKey, patErr.Key)
487+
require.Equal(t, tt.wantPattern, patErr.Pattern)
488+
require.Equal(t, tt.wantMessage, err.Error())
489+
})
490+
}
491+
}
492+
447493
type testTableGetChangeTestCase struct {
448494
name string
449495
target *Table

0 commit comments

Comments
 (0)