Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions libs/sdkgo/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
69 changes: 0 additions & 69 deletions libs/sdkgo/selector_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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.")
}
Loading