diff --git a/libs/sdkgo/selector.go b/libs/sdkgo/selector.go index 06f27564388..797571e4c2e 100644 --- a/libs/sdkgo/selector.go +++ b/libs/sdkgo/selector.go @@ -2,13 +2,10 @@ package sdkgo import ( "context" - "errors" "fmt" - "net/url" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/log" - "github.com/databricks/sdk-go/core/apierr" ) // EnvVar names the environment variable that selects the Go SDK of the commands that support @@ -48,35 +45,3 @@ func useModular(ctx context.Context, group string, defaults map[string]bool) (bo log.Debugf(ctx, "Using the %s Go SDK for %s commands", sdk, group) return modular, nil } - -// legacyHint follows the text of an internal error of the modular Go SDK. -const legacyHint = "Set " + EnvVar + "=" + legacySDK + " to use the previous Go SDK, and report the issue at https://github.com/databricks/cli/issues." - -// WithLegacyHint returns err with a hint to switch back to the legacy SDK when err is an -// internal error of the modular SDK, such as a response it could not decode. An API error, a -// failed request and a cancellation come from the server, the network or the caller, so they -// are returned as is. -func WithLegacyHint(err error) error { - if err == nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { - return err - } - if _, ok := errors.AsType[*apierr.APIError](err); ok { - return err - } - if _, ok := errors.AsType[*url.Error](err); ok { - return err - } - return modularError{err: err} -} - -type modularError struct { - err error -} - -func (e modularError) Error() string { - return e.err.Error() + "\n\n" + legacyHint -} - -func (e modularError) Unwrap() error { - return e.err -} diff --git a/libs/sdkgo/selector_test.go b/libs/sdkgo/selector_test.go index 22bc8986a45..41b338287ff 100644 --- a/libs/sdkgo/selector_test.go +++ b/libs/sdkgo/selector_test.go @@ -1,19 +1,11 @@ package sdkgo import ( - "context" - "encoding/json" - "errors" - "fmt" "maps" - "net/http" - "net/url" "slices" "testing" "github.com/databricks/cli/libs/env" - "github.com/databricks/databricks-sdk-go" - "github.com/databricks/sdk-go/core/apierr" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -94,64 +86,3 @@ func TestUseModularUsesDefaultTable(t *testing.T) { assert.Equal(t, modularByDefault[group], got, group) } } - -func TestWithLegacyHint(t *testing.T) { - var syntaxErr *json.SyntaxError - decodeErr := json.Unmarshal([]byte("not json"), &struct{}{}) - require.ErrorAs(t, decodeErr, &syntaxErr) - apiErr := apierr.FromHTTPError(http.StatusNotFound, http.Header{}, []byte(`{"error_code": "NOT_FOUND", "message": "no such file"}`)) - urlErr := &url.Error{Op: "Post", URL: "https://example.test", Err: errors.New("connection refused")} - - testCases := []struct { - name string - err error - want error - }{ - { - name: "no error", - }, - { - name: "API error", - err: apiErr, - want: apiErr, - }, - { - name: "wrapped API error", - err: fmt.Errorf("create: %w", apiErr), - want: fmt.Errorf("create: %w", apiErr), - }, - { - name: "failed request", - err: urlErr, - want: urlErr, - }, - { - name: "cancellation", - err: context.Canceled, - want: context.Canceled, - }, - { - name: "decoding error", - err: decodeErr, - want: modularError{err: decodeErr}, - }, - } - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - assert.Equal(t, tc.want, WithLegacyHint(tc.err)) - }) - } -} - -func TestWithLegacyHintOnUndecodableResponse(t *testing.T) { - server := newTestServer(t) - _, c := clients(t, server, &databricks.Config{}) - - var err error - server.record(func() { err = createModular(t.Context(), c) }, respond(http.StatusOK, nil, "not json")) - - var syntaxErr *json.SyntaxError - err = WithLegacyHint(err) - assert.ErrorAs(t, err, &syntaxErr) - assert.ErrorContains(t, err, "\n\nSet DATABRICKS_CLI_GO_SDK=legacy to use the previous Go SDK, and report the issue at https://github.com/databricks/cli/issues.") -}