Skip to content

Commit ff4974b

Browse files
authored
[Internal] Use generic error for missing clusters (#3938)
## Changes <!-- Summary of your changes that are easy to understand --> We use generic errors specified as `ErrResourceDoesNotExist` if a cluster is in invalid state or missing. ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> Unit tests - [ ] `make test` run locally - [ ] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [ ] relevant acceptance tests are passing - [ ] using Go SDK
1 parent f833cf2 commit ff4974b

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

clusters/clusters_api.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -730,14 +730,12 @@ func wrapMissingClusterError(err error, id string) error {
730730
// as is in the longer term, so that this keeps working.
731731
if apiErr.ErrorCode == "INVALID_STATE" {
732732
log.Printf("[WARN] assuming that cluster is removed on backend: %s", apiErr)
733-
apiErr.StatusCode = 404
734-
return apiErr
733+
return databricks.ErrResourceDoesNotExist
735734
}
736735
// fix non-compliant error code
737736
if strings.Contains(apiErr.Message,
738737
fmt.Sprintf("Cluster %s does not exist", id)) {
739-
apiErr.StatusCode = 404
740-
return apiErr
738+
return databricks.ErrResourceDoesNotExist
741739
}
742740
return err
743741
}

clusters/clusters_api_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package clusters
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76

87
// "reflect"
98

109
"testing"
1110

11+
"github.com/databricks/databricks-sdk-go"
1212
"github.com/databricks/databricks-sdk-go/apierr"
1313
"github.com/databricks/databricks-sdk-go/service/compute"
1414
"github.com/databricks/terraform-provider-databricks/common"
@@ -1158,15 +1158,13 @@ func TestWrapMissingClusterError(t *testing.T) {
11581158
assert.EqualError(t, wrapMissingClusterError(fmt.Errorf("x"), "abc"), "x")
11591159
assert.EqualError(t, wrapMissingClusterError(&apierr.APIError{
11601160
Message: "Cluster abc does not exist",
1161-
}, "abc"), "Cluster abc does not exist")
1161+
}, "abc"), databricks.ErrResourceDoesNotExist.Error())
11621162
}
11631163

11641164
func TestExpiredClusterAssumedAsRemoved(t *testing.T) {
11651165
err := wrapMissingClusterError(&apierr.APIError{
11661166
ErrorCode: "INVALID_STATE",
11671167
Message: "Cannot access cluster X that was terminated or unpinned more than Y days ago.",
11681168
}, "X")
1169-
var ae *apierr.APIError
1170-
assert.True(t, errors.As(err, &ae))
1171-
assert.Equal(t, 404, ae.StatusCode)
1169+
assert.EqualError(t, err, databricks.ErrResourceDoesNotExist.Error())
11721170
}

clusters/resource_cluster_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ func TestResourceClusterRead(t *testing.T) {
513513
}
514514

515515
func TestResourceClusterRead_NotFound(t *testing.T) {
516-
_, err := qa.ResourceFixture{
516+
qa.ResourceFixture{
517517
Fixtures: []qa.HTTPFixture{
518518
{
519519
Method: "GET",
@@ -531,8 +531,7 @@ func TestResourceClusterRead_NotFound(t *testing.T) {
531531
Read: true,
532532
Removed: true,
533533
ID: "abc",
534-
}.Apply(t)
535-
qa.AssertErrorStartsWith(t, err, "Cluster abc does not exist")
534+
}.ApplyNoError(t)
536535
}
537536

538537
func TestResourceClusterRead_Error(t *testing.T) {

0 commit comments

Comments
 (0)