Skip to content

Commit 5eff8e7

Browse files
committed
Better error diagnostic for SDK errors
Takes advantage of databricks/databricks-sdk-go#1261
1 parent d4d6736 commit 5eff8e7

10 files changed

Lines changed: 87 additions & 87 deletions

File tree

acceptance/bundle/resources/jobs/create-error/output.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ Warning: required field "new_cluster" is not set
66

77
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
88
Deploying resources...
9-
Error: deploying jobs.foo: creating: Method=Jobs.Create *retries.Err *apierr.APIError StatusCode=400 ErrorCode="INVALID_PARAMETER_VALUE" Message="Shared job cluster feature is only supported in multi-task jobs."
9+
Error: deploying jobs.foo: creating: Shared job cluster feature is only supported in multi-task jobs. (400 INVALID_PARAMETER_VALUE)
10+
11+
Endpoint: POST [DATABRICKS_URL]/api/2.2/jobs/create
12+
HTTP Status: 400 Bad Request
13+
API error_code: INVALID_PARAMETER_VALUE
14+
API message: Shared job cluster feature is only supported in multi-task jobs.
1015

1116
Updating deployment state...
1217

bundle/terranova/tnresources/app.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (r *ResourceApp) DoCreate(ctx context.Context) (string, error) {
3333
}
3434
waiter, err := r.client.Apps.Create(ctx, request)
3535
if err != nil {
36-
return "", SDKError{Method: "Apps.Create", Err: err}
36+
return "", err
3737
}
3838

3939
// TODO: Store waiter for Wait method
@@ -48,7 +48,7 @@ func (r *ResourceApp) DoUpdate(ctx context.Context, id string) error {
4848
}
4949
response, err := r.client.Apps.Update(ctx, request)
5050
if err != nil {
51-
return SDKError{Method: "Apps.Update", Err: err}
51+
return err
5252
}
5353

5454
if response.Name != id {

bundle/terranova/tnresources/job.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (r *ResourceJob) DoCreate(ctx context.Context) (string, error) {
3333
}
3434
response, err := r.client.Jobs.Create(ctx, request)
3535
if err != nil {
36-
return "", SDKError{Method: "Jobs.Create", Err: err}
36+
return "", err
3737
}
3838
return strconv.FormatInt(response.JobId, 10), nil
3939
}
@@ -43,23 +43,15 @@ func (r *ResourceJob) DoUpdate(ctx context.Context, id string) error {
4343
if err != nil {
4444
return err
4545
}
46-
err = r.client.Jobs.Reset(ctx, request)
47-
if err != nil {
48-
return SDKError{Method: "Jobs.Reset", Err: err}
49-
}
50-
return nil
46+
return r.client.Jobs.Reset(ctx, request)
5147
}
5248

5349
func DeleteJob(ctx context.Context, client *databricks.WorkspaceClient, id string) error {
5450
idInt, err := strconv.ParseInt(id, 10, 64)
5551
if err != nil {
5652
return err
5753
}
58-
err = client.Jobs.DeleteByJobId(ctx, idInt)
59-
if err != nil {
60-
return SDKError{Method: "Jobs.DeleteByJobId", Err: err}
61-
}
62-
return nil
54+
return client.Jobs.DeleteByJobId(ctx, idInt)
6355
}
6456

6557
func (r *ResourceJob) WaitAfterCreate(ctx context.Context) error {

bundle/terranova/tnresources/pipeline.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (r *ResourcePipeline) Config() any {
2727
func (r *ResourcePipeline) DoCreate(ctx context.Context) (string, error) {
2828
response, err := r.client.Pipelines.Create(ctx, r.config)
2929
if err != nil {
30-
return "", SDKError{Method: "Pipelines.Create", Err: err}
30+
return "", err
3131
}
3232
return response.PipelineId, nil
3333
}
@@ -68,19 +68,11 @@ func (r *ResourcePipeline) DoUpdate(ctx context.Context, id string) error {
6868
ForceSendFields: filterFields[pipelines.EditPipeline](r.config.ForceSendFields),
6969
}
7070

71-
err := r.client.Pipelines.Update(ctx, request)
72-
if err != nil {
73-
return SDKError{Method: "Pipelines.Update", Err: err}
74-
}
75-
return nil
71+
return r.client.Pipelines.Update(ctx, request)
7672
}
7773

7874
func DeletePipeline(ctx context.Context, client *databricks.WorkspaceClient, id string) error {
79-
err := client.Pipelines.DeleteByPipelineId(ctx, id)
80-
if err != nil {
81-
return SDKError{Method: "Pipelines.DeleteByPipelineId", Err: err}
82-
}
83-
return nil
75+
return client.Pipelines.DeleteByPipelineId(ctx, id)
8476
}
8577

8678
func (r *ResourcePipeline) WaitAfterCreate(ctx context.Context) error {

bundle/terranova/tnresources/schema.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (r *ResourceSchema) Config() any {
2828
func (r *ResourceSchema) DoCreate(ctx context.Context) (string, error) {
2929
response, err := r.client.Schemas.Create(ctx, r.config)
3030
if err != nil {
31-
return "", SDKError{Method: "Schemas.Create", Err: err}
31+
return "", err
3232
}
3333
return response.FullName, nil
3434
}
@@ -46,7 +46,7 @@ func (r *ResourceSchema) DoUpdate(ctx context.Context, id string) error {
4646

4747
response, err := r.client.Schemas.Update(ctx, updateRequest)
4848
if err != nil {
49-
return SDKError{Method: "Schemas.Update", Err: err}
49+
return err
5050
}
5151

5252
if response.FullName != id {
@@ -57,15 +57,11 @@ func (r *ResourceSchema) DoUpdate(ctx context.Context, id string) error {
5757
}
5858

5959
func DeleteSchema(ctx context.Context, client *databricks.WorkspaceClient, id string) error {
60-
err := client.Schemas.Delete(ctx, catalog.DeleteSchemaRequest{
60+
return client.Schemas.Delete(ctx, catalog.DeleteSchemaRequest{
6161
FullName: id,
6262
Force: true,
6363
ForceSendFields: nil,
6464
})
65-
if err != nil {
66-
return SDKError{Method: "Schemas.Delete", Err: err}
67-
}
68-
return nil
6965
}
7066

7167
func (r *ResourceSchema) WaitAfterCreate(ctx context.Context) error {

bundle/terranova/tnresources/sdk_error.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

bundle/terranova/tnresources/sql_warehouse.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (r *ResourceSqlWarehouse) Config() any {
2828
func (r *ResourceSqlWarehouse) DoCreate(ctx context.Context) (string, error) {
2929
waiter, err := r.client.Warehouses.Create(ctx, r.config)
3030
if err != nil {
31-
return "", SDKError{Method: "Warehouses.Create", Err: err}
31+
return "", err
3232
}
3333

3434
return waiter.Id, nil
@@ -55,7 +55,7 @@ func (r *ResourceSqlWarehouse) DoUpdate(ctx context.Context, id string) error {
5555

5656
waiter, err := r.client.Warehouses.Edit(ctx, request)
5757
if err != nil {
58-
return SDKError{Method: "Warehouses.Edit", Err: err}
58+
return err
5959
}
6060

6161
if waiter.Id != id {
@@ -76,9 +76,5 @@ func (r *ResourceSqlWarehouse) WaitAfterUpdate(ctx context.Context) error {
7676
}
7777

7878
func DeleteSqlWarehouse(ctx context.Context, client *databricks.WorkspaceClient, oldID string) error {
79-
err := client.Warehouses.DeleteById(ctx, oldID)
80-
if err != nil {
81-
return SDKError{Method: "Warehouses.DeleteById", Err: err}
82-
}
83-
return nil
79+
return client.Warehouses.DeleteById(ctx, oldID)
8480
}

bundle/terranova/tnresources/volume.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (r *ResourceVolume) Config() any {
3232
func (r *ResourceVolume) DoCreate(ctx context.Context) (string, error) {
3333
response, err := r.client.Volumes.Create(ctx, r.config)
3434
if err != nil {
35-
return "", SDKError{Method: "Volumes.Create", Err: err}
35+
return "", err
3636
}
3737
return response.FullName, nil
3838
}
@@ -58,7 +58,7 @@ func (r *ResourceVolume) DoUpdate(ctx context.Context, id string) error {
5858

5959
response, err := r.client.Volumes.Update(ctx, updateRequest)
6060
if err != nil {
61-
return SDKError{Method: "Volumes.Update", Err: err}
61+
return err
6262
}
6363

6464
if id != response.FullName {
@@ -91,7 +91,7 @@ func (r *ResourceVolume) DoUpdateWithID(ctx context.Context, id string) (string,
9191

9292
response, err := r.client.Volumes.Update(ctx, updateRequest)
9393
if err != nil {
94-
return "", SDKError{Method: "Volumes.Update", Err: err}
94+
return "", err
9595
}
9696

9797
return response.FullName, nil
@@ -100,7 +100,7 @@ func (r *ResourceVolume) DoUpdateWithID(ctx context.Context, id string) (string,
100100
func DeleteVolume(ctx context.Context, client *databricks.WorkspaceClient, id string) error {
101101
err := client.Volumes.DeleteByName(ctx, id)
102102
if err != nil {
103-
return SDKError{Method: "Volumes.Delete", Err: err}
103+
return err
104104
}
105105
return nil
106106
}

libs/diag/diagnostic.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func FromErr(err error) Diagnostics {
4848
return []Diagnostic{
4949
{
5050
Severity: Error,
51-
Summary: err.Error(),
51+
Summary: FormatAPIErrorSummary(err),
52+
Detail: FormatAPIErrorDetails(err),
5253
},
5354
}
5455
}

libs/diag/sdk_error.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package diag
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
9+
"github.com/databricks/databricks-sdk-go/apierr"
10+
)
11+
12+
func findApiErr(e error) *apierr.APIError {
13+
for {
14+
cast, ok := e.(*apierr.APIError)
15+
if ok {
16+
return cast
17+
}
18+
19+
inner := errors.Unwrap(e)
20+
if inner == nil {
21+
break
22+
}
23+
e = inner
24+
}
25+
return nil
26+
}
27+
28+
func FormatAPIErrorSummary(e error) string {
29+
extra := ""
30+
apiErr := findApiErr(e)
31+
if apiErr != nil {
32+
extra = strings.TrimSpace(fmt.Sprintf("%d %s", apiErr.StatusCode, apiErr.ErrorCode))
33+
extra = " (" + extra + ")"
34+
}
35+
36+
return e.Error() + extra
37+
}
38+
39+
func FormatAPIErrorDetails(e error) string {
40+
apiErr := findApiErr(e)
41+
if apiErr == nil {
42+
return ""
43+
}
44+
endpoint := "n/a"
45+
httpStatus := ""
46+
w := apiErr.ResponseWrapper
47+
if w != nil {
48+
resp := w.Response
49+
if resp != nil {
50+
httpStatus = resp.Status
51+
req := resp.Request
52+
if req != nil {
53+
endpoint = fmt.Sprintf("%s %s", req.Method, req.URL)
54+
}
55+
}
56+
}
57+
if len(httpStatus) == 0 {
58+
httpStatus = strconv.Itoa(apiErr.StatusCode)
59+
}
60+
return fmt.Sprintf("Endpoint: %s\nHTTP Status: %s\nAPI error_code: %s\nAPI message: %s", endpoint, httpStatus, apiErr.ErrorCode, apiErr.Message)
61+
}

0 commit comments

Comments
 (0)