-
Notifications
You must be signed in to change notification settings - Fork 244
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
Return EventsAPIV2Error from Events V2 API call. #368
Conversation
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 raising this PR. If you think there's value in returning a similar error value as the REST API, I'm not against that being added to the repo. That said, the type you're trying to do it with is really designed to work with the REST API only, and so it doesn't feel right to include it on the Events API.
How would you feel about creating a new type, maybe with a name like EventsAPIError
?
@abhishekrb19 might you have some time soon to look at the requested changes? |
@theckman, thank you for the review and reminder. I adapted your recommendation to use |
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.
I've noticed a few things that should probably be adjusted before merging this PR.
I did also notice that the ManageEvent*
method isn't included in this change, but it will need to be. I think to do this we'll need to create separate checkResponse
and getErrorFromResponse
methods for the Events V2 API calls within the client.go
file.
// If the API response did not contain an error object, the .Valid field of | ||
// EventsAPIV2Error will be false. If .Valid is true, the .ErrorObject field is | ||
// valid and should be consulted. | ||
EventsAPIV2Error NullEventsAPIV2ErrorObject |
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 you be willing to make this struct field APIError
. While the names aligned in the other type, it was more to make it clear it wasn't just a standard Go error (by the name).
} | ||
|
||
var _ error = &EventsAPIV2Error{} // assert that it satisfies the error interface. | ||
var _ json.Unmarshaler = &EventsAPIV2Error{} // assert that it satisfies the json.Unmarshaler interface. |
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.
👍 here too, but also could you change this a little bit:
var _ json.Unmarshaler = (*EventsAPIV2Error)(nil)
@@ -546,7 +550,7 @@ func (c *Client) decodeJSON(resp *http.Response, payload interface{}) error { | |||
|
|||
func (c *Client) checkResponse(resp *http.Response, err error) (*http.Response, error) { | |||
if err != nil { | |||
return resp, fmt.Errorf("Error calling the API endpoint: %v", err) | |||
return resp, fmt.Errorf("error calling the API endpoint: %v", err) |
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.
Even though I personally agree with this change, could you undo it? I plan to lowercase all of the errors for v2, but the style of this library (within v1) is to capitalize the first word of an error message.
e.EventsAPIV2Error.ErrorObject.Message, | ||
e.EventsAPIV2Error.ErrorObject.Status, | ||
apiErrorsDetailString(e.EventsAPIV2Error.ErrorObject.Errors), | ||
) |
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.
If the Errors
slice is empty, this will print HTTP response failed with status code: CODE, message: MSG, status: STATUS:
.
That doesn't seem like a very ideal error message, so could you handle the empty errors case separately to avoid the dangling :
?
@@ -165,7 +169,7 @@ func (a APIError) Error() string { | |||
func apiErrorsDetailString(errs []string) string { | |||
switch n := len(errs); n { | |||
case 0: | |||
panic("errs slice is empty") | |||
return "" |
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.
I think this should be returned back to a panic()
, especially when the the dangling :
issue is addressed below and so this should never receive a 0
length slice.
// thus crashing their whole program. | ||
type NullAPIErrorObject struct { | ||
Valid bool | ||
ErrorObject APIErrorObject | ||
} | ||
|
||
var _ json.Unmarshaler = &NullAPIErrorObject{} // assert that it satisfies the json.Unmarshaler interface. |
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.
👍, but also could you change this a little bit:
var _ json.Unmarshaler = (*NullAPIErrorObject)(nil)
message string | ||
} | ||
|
||
var _ error = &EventsAPIV2Error{} // assert that it satisfies the error interface. |
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.
I don't think this assertion is necessary as the compiler will flag it elsewhere. The json.Unmarshaler
one is a good check though, because that wouldn't be caught until runtime. Could you remove just this error one?
@@ -135,6 +137,8 @@ type APIError struct { | |||
message string | |||
} | |||
|
|||
var _ error = &APIError{} // assert that it implements the error interface. |
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.
Like the other I commented on below I'm not sure there's much value in this specific interface check, could you remove it?
|
||
// UnmarshalJSON satisfies encoding/json.Unmarshaler. | ||
func (e *EventsAPIV2Error) UnmarshalJSON(data []byte) error { | ||
var eaeo EventsAPIV2ErrorObject |
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.
LOL. Say that five times fast...
While you're in there making changes, I'm ok with just eo
. But you don't need to change it.
@abhishekrb19 might you have the time to make the requested changes, so that this can make it into v1.5.0? |
The original author didn't seem to have the spare cycles to address the feedback on the PR, so I squashed their commits into one and then added this commit to address the feedback.
The original author didn't seem to have the spare cycles to address the feedback on the PR, so I squashed their commits into one and then added this commit to address the feedback.
@theckman, sorry for not getting back earlier! Thank you for addressing the comments! |
This patch does the following:
EventsAPIV2Error
instead oferror
for the Events V2 API call. This will be consistent with the other REST API calls and callers could leverage the wrapper methods added such as.BadRequest()
,.RateLimited()
,and
.Temporary()
. Add unit tests for the same.