|
| 1 | +package diag |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/databricks/databricks-sdk-go/apierr" |
| 11 | + "github.com/databricks/databricks-sdk-go/common" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestFormatAPIError(t *testing.T) { |
| 16 | + u, _ := url.Parse("https://example.com/api/2.0/foo") |
| 17 | + request := &http.Request{Method: http.MethodPost, URL: u} |
| 18 | + |
| 19 | + cases := []struct { |
| 20 | + name string |
| 21 | + err error |
| 22 | + expectedSummary string |
| 23 | + expectedDetails string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "plain error (no apierr)", |
| 27 | + err: errors.New("foo"), |
| 28 | + expectedSummary: "foo", |
| 29 | + expectedDetails: "", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "apierr no wrapper", |
| 33 | + err: &apierr.APIError{ |
| 34 | + Message: "api error message", |
| 35 | + ErrorCode: "TEST_ERROR", |
| 36 | + StatusCode: 400, |
| 37 | + }, |
| 38 | + expectedSummary: "api error message (400 TEST_ERROR)", |
| 39 | + expectedDetails: "Endpoint: n/a\nHTTP Status: 400\nAPI error_code: TEST_ERROR\nAPI message: api error message", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "apierr with Response only", |
| 43 | + err: &apierr.APIError{ |
| 44 | + Message: "api error message", |
| 45 | + ErrorCode: "TEST_ERROR", |
| 46 | + StatusCode: 400, |
| 47 | + ResponseWrapper: &common.ResponseWrapper{ |
| 48 | + Response: &http.Response{Status: "400 Bad Request"}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + expectedSummary: "api error message (400 TEST_ERROR)", |
| 52 | + expectedDetails: "Endpoint: n/a\nHTTP Status: 400 Bad Request\nAPI error_code: TEST_ERROR\nAPI message: api error message", |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "apierr with full Response+Request", |
| 56 | + err: &apierr.APIError{ |
| 57 | + Message: "api error message", |
| 58 | + ErrorCode: "TEST_ERROR", |
| 59 | + StatusCode: 400, |
| 60 | + ResponseWrapper: &common.ResponseWrapper{ |
| 61 | + Response: &http.Response{Status: "400 Bad Request", Request: request}, |
| 62 | + }, |
| 63 | + }, |
| 64 | + expectedSummary: "api error message (400 TEST_ERROR)", |
| 65 | + expectedDetails: "Endpoint: POST https://example.com/api/2.0/foo\nHTTP Status: 400 Bad Request\nAPI error_code: TEST_ERROR\nAPI message: api error message", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "wrapped apierr", |
| 69 | + err: fmt.Errorf("wrapped: %w", &apierr.APIError{ |
| 70 | + Message: "api error message", |
| 71 | + ErrorCode: "TEST_ERROR", |
| 72 | + StatusCode: 500, |
| 73 | + }), |
| 74 | + expectedSummary: "wrapped: api error message (500 TEST_ERROR)", |
| 75 | + expectedDetails: "Endpoint: n/a\nHTTP Status: 500\nAPI error_code: TEST_ERROR\nAPI message: api error message", |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for _, tc := range cases { |
| 80 | + t.Run(tc.name, func(t *testing.T) { |
| 81 | + assert.Equal(t, tc.expectedSummary, FormatAPIErrorSummary(tc.err)) |
| 82 | + assert.Equal(t, tc.expectedDetails, FormatAPIErrorDetails(tc.err)) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments