-
Notifications
You must be signed in to change notification settings - Fork 35
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
task: enable thelper and errorlint #225
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,24 +169,6 @@ type ListOptions struct { | |
ItemsPerPage int `url:"itemsPerPage,omitempty"` | ||
} | ||
|
||
// ErrorResponse reports the error caused by an API request. | ||
type ErrorResponse struct { | ||
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. moved closer to the implementation of |
||
// HTTP response that caused this error | ||
Response *http.Response | ||
|
||
// The error code as specified in https://docs.atlas.mongodb.com/reference/api/api-errors/ | ||
ErrorCode string `json:"errorCode"` | ||
|
||
// HTTP status code. | ||
HTTPCode int `json:"error"` | ||
|
||
// A short description of the error, which is simply the HTTP status phrase. | ||
Reason string `json:"reason"` | ||
|
||
// A more detailed description of the error. | ||
Detail string `json:"detail,omitempty"` | ||
} | ||
|
||
func (resp *Response) getCurrentPageLink() (*Link, error) { | ||
if link := resp.getLinkByRef("self"); link != nil { | ||
return link, nil | ||
|
@@ -222,7 +204,7 @@ func (resp *Response) CurrentPage() (int, error) { | |
|
||
pageNum, err := strconv.Atoi(pageNumStr) | ||
if err != nil { | ||
return 0, fmt.Errorf("error getting current page: %s", err) | ||
return 0, fmt.Errorf("error getting current page: %w", err) | ||
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.
|
||
} | ||
|
||
return pageNum, nil | ||
|
@@ -360,7 +342,7 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body int | |
} | ||
|
||
// newEncodedBody returns an ReadWriter object containing the body of the http request. | ||
func (c *Client) newEncodedBody(body interface{}) (io.Reader, error) { | ||
func newEncodedBody(body interface{}) (io.Reader, error) { | ||
buf := &bytes.Buffer{} | ||
enc := json.NewEncoder(buf) | ||
enc.SetEscapeHTML(false) | ||
|
@@ -407,7 +389,7 @@ func (c *Client) newRequest(ctx context.Context, urlStr, method string, body int | |
|
||
var buf io.Reader | ||
if body != nil { | ||
if buf, err = c.newEncodedBody(body); err != nil { | ||
if buf, err = newEncodedBody(body); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
@@ -476,7 +458,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res | |
} | ||
} else { | ||
decErr := json.NewDecoder(resp.Body).Decode(v) | ||
if decErr == io.EOF { | ||
if errors.Is(decErr, io.EOF) { | ||
decErr = nil // ignore EOF errors caused by empty response body | ||
} | ||
if decErr != nil { | ||
|
@@ -488,11 +470,35 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res | |
return response, err | ||
} | ||
|
||
// ErrorResponse reports the error caused by an API request. | ||
type ErrorResponse struct { | ||
// Response that caused this error | ||
Response *http.Response | ||
// ErrorCode is the code as specified in https://docs.atlas.mongodb.com/reference/api/api-errors/ | ||
ErrorCode string `json:"errorCode"` | ||
// HTTPCode status code. | ||
HTTPCode int `json:"error"` | ||
// Reason is short description of the error, which is simply the HTTP status phrase. | ||
Reason string `json:"reason"` | ||
// Detail is more detailed description of the error. | ||
Detail string `json:"detail,omitempty"` | ||
} | ||
|
||
func (r *ErrorResponse) Error() string { | ||
return fmt.Sprintf("%v %v: %d (request %q) %v", | ||
r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.ErrorCode, r.Detail) | ||
} | ||
|
||
func (r *ErrorResponse) Is(target error) bool { | ||
var v *ErrorResponse | ||
|
||
return errors.As(target, &v) && | ||
r.ErrorCode == v.ErrorCode && | ||
r.HTTPCode == v.HTTPCode && | ||
r.Reason == v.Reason && | ||
r.Detail == v.Detail | ||
} | ||
|
||
// CheckResponse checks the API response for errors, and returns them if present. A response is considered an | ||
// error if it has a status code outside the 200 range. API error responses are expected to have either no response | ||
// body, or a JSON response body that maps to ErrorResponse. Any other response body will be silently ignored. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thank you for adding the description for each linter!