Improve error response unmarshalling #725
Merged
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.
Looking through some of the code, I noticed errors were manually unmarshalled using a ton of type assertions and maps. That's not good. The problem was that the error types don't export their underlying
stripeErr
fields.json.Unmarshal
uses reflection and tags to find fields that are exported (otherwise it can't set values). I've created an un-exportedrawErr
type that can be used for unmarshalling, and a further typeinternalError
that embeds the Error type as it is, and adds thedecline_code
field at the same level (to correspond to the error format).Ideally, the
decline_code
field should be a part of the mainError
type, so we don't need that second internal type. The type ofdecline_code
was (and is)string
. I've left it as is to not break any existing code, but if a field is tagged asomitempty
, a pointer type should be used.Tests all pass, no behavioural changes. The code is just a bit less verbose, more readable, easier to maintain, less error-prone, and possibly faster (though I didn't write a Bench test).