Skip to content

Commit

Permalink
Unmarshal Error based on Backend type
Browse files Browse the repository at this point in the history
  • Loading branch information
cjavilla-stripe committed Jul 25, 2019
1 parent e90bee1 commit 9f0d44d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,16 +462,20 @@ func (s *BackendImplementation) Do(req *http.Request, body *bytes.Buffer, v inte

// ResponseToError converts a stripe response to an Error.
func (s *BackendImplementation) ResponseToError(res *http.Response, resBody []byte) error {

var raw rawError
if err := s.UnmarshalJSONVerbose(res.StatusCode, resBody, &raw); err != nil {
// If deserializing the error json fails at the top level, we retry
// serializing at the error level because the shape of error response body
// for the API is different from the OAuth error response.
if s.Type == ConnectBackend {
// If this is an OAuth request, deserialize as Error because OAuth errors
// are a different shape from the standard API errors.
var topLevelError rawErrorInternal
if err := s.UnmarshalJSONVerbose(res.StatusCode, resBody, &topLevelError); err != nil {
return err
}
raw.E = &topLevelError
} else {
if err := s.UnmarshalJSONVerbose(res.StatusCode, resBody, &raw); err != nil {
return err
}
}

// no error in resBody
Expand Down

0 comments on commit 9f0d44d

Please sign in to comment.