VictorAvelar / mollie-api-go

Golang wrapper for Mollie's REST API with full resource coverage.
https://www.mollie.com/developers/libraries/golang
MIT License
63 stars 37 forks source link

Can't access error response from Client payment creation #47

Closed Icety closed 4 years ago

Icety commented 4 years ago

When a new Client-Payment is created and an error occured a custom mollie error is returned. I have deemed it impossible so far to get the returned error response body from this call, which shows me what I do wrong exactly. How can I access the error response body? The current response body is marshalled to a Payment object, but this does not contain the error fields, so shows nothing.

To Reproduce Call the Create method on mollie.Client with faulty data. The following error is returned, where the Response can't be accessed. image

VictorAvelar commented 4 years ago

Hi, thanks for reporting this, just to make sure I fully understand the problem and what might be a good solution for it, you will like that next to the error and the payment objects, also the raw response is returned as received from the server?

Do you see any possible solution different to what I described above?

Icety commented 4 years ago

Thanks for the quick reply! I think there is no need for a third response. Since you already use a custom error object, I would suggest to add a 4th field to the error that contains the body of the message. You do pass along the response object, but because you closed it already, it can't be accessed to parse the body yourself. eg:

type Error struct {
    Code     int            `json:"code"`
    Message  string         `json:"message"`
    Response *http.Response `json:"response"` // the full response that produced the error
    Body  []byte `json:"body"`
} 
VictorAvelar commented 4 years ago

Thanks for the quick reply! I think there is no need for a third response. Since you already use a custom error object, I would suggest to add a 4th field to the error that contains the body of the message. You do pass along the response object, but because you closed it already, it can't be accessed to parse the body yourself. eg:

type Error struct {
  Code     int            `json:"code"`
  Message  string         `json:"message"`
  Response *http.Response `json:"response"` // the full response that produced the error
  Body  []byte `json:"body"`
} 

That will require touching almost all of the library, I think for me it might be better to reopen the response body using ioutil.NopCloser, which will solve the problem at least in theory.

Icety commented 4 years ago

Ahh, yeah, I understand that. Opening it again, should then probably do the trick.

VictorAvelar commented 4 years ago

I will take a closer look tomorrow, hopefully, I will have enough time to draft a PR.

I'll keep this issue updated 😉

Icety commented 4 years ago

Thanks, that would be great!

VictorAvelar commented 4 years ago

Hey @Icety I've an initial draft, it took more work than expected but I am actively looking into it, just to keep you posted. 😄

VictorAvelar commented 4 years ago

@Icety can you pull the branch and let me know if your problem is fixed?? 👍 thanks in advance.