Parse ruby error response body and add attributes #326
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.
Description
This PR is based on a PR I originally opened on the generated mailchimp-marketing-ruby gem in 2021. At the time I didn't know that the gem was generated and the PR was rejected several months later. I never got around to submitting the patch to this project and have been using my own fork. However, I would like to see if my change could make it into this repo.
This PR should generally be backwards compatible, with the exception that the error message won't show a stringified version of a ruby hash.
Here's the summary from the original PR on the wrong repo:
The
MailchimpMarketing::ApiError
is used to wrap up error responses from the Mailchimp API. This class is used to raise errors encountered by theApiClient
class. In the case that we have a response body, the body's json is parsed into a hash and this hash is passed in as a named argument to theApiError
constructor. In the case that a hash is provided to theApiErrors
constructor (and the hash doesn't have a:message
key), it is simply passed toStandardError
constructor.Because a hash is being passed to the
StandardError
constructor, themessage
value of the resulting error is the result of callingto_s
on the hash. EG:Note that the value of
message
is definitely not JSON. 😄The
ApiError
class' constructor does take all of the named values provided to the constructor and store them as instance variables.It would be nice to have access to the response body, if available. I do not feel comfortable calling
eval
on the hash stored inmessage
, especially since this could potentially be a string. I do not want to useinstance_variable_get
to read the instance variable, since this would break encapsulation.As such, this PR simply introduces a new
attr_reader
for:response_headers
and:response_body
. Now this data is safely accessible from outside of the error class.As a note, there are no uses of
:response_header
in this gem. However, the comments in theApiError
comments show this as an example option, so I added the reader. This doesn't cover any cases where other non-standard attributes are provided to the constructor.Known Issues