-
Notifications
You must be signed in to change notification settings - Fork 186
Include more details from SDK/API errors in the diagnostics #3379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e02074a
8f1b2cc
869a696
a8f0088
7b9c3f6
3173862
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package diag | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/databricks-sdk-go/apierr" | ||
| ) | ||
|
|
||
| func FormatAPIErrorSummary(e error) string { | ||
| var apiErr *apierr.APIError | ||
| if !errors.As(e, &apiErr) { | ||
| return e.Error() | ||
| } | ||
| extra := strings.TrimSpace(fmt.Sprintf("%d %s", apiErr.StatusCode, apiErr.ErrorCode)) | ||
| return e.Error() + " (" + extra + ")" | ||
| } | ||
|
|
||
| func FormatAPIErrorDetails(e error) string { | ||
| var apiErr *apierr.APIError | ||
| if !errors.As(e, &apiErr) { | ||
| return "" | ||
| } | ||
|
|
||
| endpoint := "n/a" | ||
| httpStatus := "" | ||
| w := apiErr.ResponseWrapper | ||
| if w != nil { | ||
| resp := w.Response | ||
| if resp != nil { | ||
| httpStatus = resp.Status | ||
| req := resp.Request | ||
| if req != nil { | ||
| endpoint = fmt.Sprintf("%s %s", req.Method, req.URL) | ||
| } | ||
| } | ||
| } | ||
| if len(httpStatus) == 0 { | ||
| httpStatus = strconv.Itoa(apiErr.StatusCode) | ||
| } | ||
| return fmt.Sprintf("Endpoint: %s\nHTTP Status: %s\nAPI error_code: %s\nAPI message: %s", endpoint, httpStatus, apiErr.ErrorCode, apiErr.Message) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the API message always present or can it be empty?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
They are not guaranteed to be set. I recall that the SDK does some work to populate them with HTTP error codes if there is no actual response, but double check...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, SDK tries very hard to populate those (e.g. it parses text and html) and falls back to setting them to UNKNOWN / full body. So, if backend returns html or text, "API error_code" and "API message" labels are not strictly correct or rather they are represent SDK's view of error_code and message. However, 99% of cases would get JSON error so "API error_code/message" does refer to error_code/message in the response. This is something that is mentioned in the docs, unlike "error code" which is vague. BTW, this message is just a reasonably simple starting point. I think this message can be further improved in follow up PRs:
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to have a unit test for this to check the output when there is or no ResponseWrapper/Request/etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea, added here 5123233