From 6ea2cce1fadb1d1970422671421597e1d97b634d Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Sun, 10 Jul 2022 11:53:04 -0400 Subject: [PATCH] sql/schemachanger/scbuild: minor cleanup Improves the error handling a tad to make runtime errors and assertion failure. Fixes a typo. Improves testing output. Release note: None --- pkg/sql/schemachanger/scbuild/build.go | 21 ++++++++++++------- pkg/sql/schemachanger/scbuild/builder_test.go | 4 ++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pkg/sql/schemachanger/scbuild/build.go b/pkg/sql/schemachanger/scbuild/build.go index 5f2c515f0a51..355f9265d7d2 100644 --- a/pkg/sql/schemachanger/scbuild/build.go +++ b/pkg/sql/schemachanger/scbuild/build.go @@ -12,6 +12,7 @@ package scbuild import ( "context" + "runtime" "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/catalog" @@ -62,12 +63,18 @@ func Build( SchemaFeatureChecker: dependencies.FeatureChecker(), } defer func() { - if recErr := recover(); recErr != nil { - if errObj, ok := recErr.(error); ok { - err = errObj - } else { - err = errors.Errorf("unexpected error encountered while building schema change plan %s", recErr) - } + switch recErr := recover().(type) { + case nil: + // No error. + case runtime.Error: + err = errors.WithAssertionFailure(recErr) + case error: + err = recErr + default: + err = errors.AssertionFailedf( + "unexpected error encountered while building schema change plan %s", + recErr, + ) } }() scbuildstmt.Process(b, an.GetStatement()) @@ -83,7 +90,7 @@ func Build( for _, e := range bs.output { if e.metadata.Size() == 0 { // Exclude targets which weren't explicitly set. - // Explicity-set targets have non-zero values in the target metadata. + // Explicitly-set targets have non-zero values in the target metadata. continue } ts.Targets = append(ts.Targets, scpb.MakeTarget(e.target, e.element, &e.metadata)) diff --git a/pkg/sql/schemachanger/scbuild/builder_test.go b/pkg/sql/schemachanger/scbuild/builder_test.go index d887693a923f..0ac5d83e4309 100644 --- a/pkg/sql/schemachanger/scbuild/builder_test.go +++ b/pkg/sql/schemachanger/scbuild/builder_test.go @@ -122,7 +122,7 @@ func run( var tableID descpb.ID tdb.QueryRow(t, fmt.Sprintf(`SELECT '%s'::REGCLASS::INT`, tableName)).Scan(&tableID) if tableID == 0 { - t.Fatalf("failed to read ID of new table %s", tableName) + d.Fatalf(t, "failed to read ID of new table %s", tableName) } t.Logf("created relation with id %d", tableID) } @@ -142,7 +142,7 @@ func run( require.NoError(t, err) for i := range stmts { output, err = scbuild.Build(ctx, deps, output, stmts[i].AST) - require.NoErrorf(t, err, "%s", stmts[i].SQL) + require.NoErrorf(t, err, "%s: %s", d.Pos, stmts[i].SQL) } }) return marshalState(t, output)