go-resty / resty

Simple HTTP and REST client library for Go
MIT License
10.12k stars 710 forks source link

apply OnAfterResponse middleware when got err response #389

Closed git-hulk closed 3 years ago

git-hulk commented 3 years ago

hey, OnAfterResponse middleware only applied when the HTTP response without error, does it make sense that also applies it on error?

felixd commented 3 years ago

Can you please clarify your question. It does work even when HTTP response with an error. Below you can find part of my code:

    // After REST response is received
    //  Checking for REST error response.
    // https://github.com/go-resty/resty/blob/master/client.go#L382
    tb.resty.OnAfterResponse(func(_ *resty.Client, r *resty.Response) error {
        if r.IsError() {
            // TODO: Analyze refreshToken method
            if r.StatusCode() == 401 {

Ok, you might be talking about this issue: https://github.com/go-resty/resty/issues/356

I will check that on my side as well.

git-hulk commented 3 years ago

hi, @felixd,

thanks for your reply, sorry that I didn't explain the question clearly. My question was thatOnAfterResponse hook wasn't invoked when occurs the error like network/DNS failure, since the resty would return the error before invoking the hook in below:

https://github.com/go-resty/resty/blob/master/client.go#L837 https://github.com/go-resty/resty/blob/master/client.go#L860

git-hulk commented 3 years ago

Can you please clarify your question. It does work even when HTTP response with an error. Below you can find part of my code:

  // After REST response is received
  //  Checking for REST error response.
  // https://github.com/go-resty/resty/blob/master/client.go#L382
  tb.resty.OnAfterResponse(func(_ *resty.Client, r *resty.Response) error {
      if r.IsError() {
          // TODO: Analyze refreshToken method
          if r.StatusCode() == 401 {

Ok, you might be talking about this issue: #356

I will check that on my side as well.

yeah, close this issue as a duplicate