simplify rsp parse #108

Merged
vtolstov merged 1 commits from logrsp into v3 2023-03-16 19:17:12 +03:00
2 changed files with 49 additions and 70 deletions

View File

@ -2,11 +2,9 @@ package http
import ( import (
"bufio" "bufio"
"bytes"
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"sync" "sync"
@ -121,59 +119,55 @@ func (h *httpStream) Close() error {
func (h *httpStream) parseRsp(ctx context.Context, log logger.Logger, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error { func (h *httpStream) parseRsp(ctx context.Context, log logger.Logger, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error {
var err error var err error
var buf []byte
// fast path return
if hrsp.StatusCode == http.StatusNoContent {
return nil
}
select { select {
case <-ctx.Done(): case <-ctx.Done():
err = ctx.Err() err = ctx.Err()
default: default:
// fast path return if hrsp.Body != nil {
if hrsp.StatusCode == http.StatusNoContent { buf, err = io.ReadAll(hrsp.Body)
return nil if err != nil {
if log.V(logger.ErrorLevel) {
log.Errorf(ctx, "failed to read body: %v", err)
}
return errors.InternalServerError("go.micro.client", string(buf))
}
}
if log.V(logger.DebugLevel) {
log.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
} }
if hrsp.StatusCode < 400 { if hrsp.StatusCode < 400 {
if log.V(logger.DebugLevel) { if err = cf.Unmarshal(buf, rsp); err != nil {
buf, rerr := io.ReadAll(hrsp.Body)
log.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
if err != nil {
return errors.InternalServerError("go.micro.client", rerr.Error())
}
hrsp.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
}
if err = cf.ReadBody(hrsp.Body, rsp); err != nil {
return errors.InternalServerError("go.micro.client", err.Error()) return errors.InternalServerError("go.micro.client", err.Error())
} }
return nil return nil
} }
var rerr interface{}
errmap, ok := opts.Context.Value(errorMapKey{}).(map[string]interface{}) errmap, ok := opts.Context.Value(errorMapKey{}).(map[string]interface{})
if ok && errmap != nil { if ok && errmap != nil {
if err, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)].(error); !ok { if rerr, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)].(error); !ok {
err, ok = errmap["default"].(error) rerr, ok = errmap["default"].(error)
} }
} }
if !ok || err == nil { if !ok || rerr == nil {
buf, cerr := io.ReadAll(hrsp.Body)
if log.V(logger.DebugLevel) {
log.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
}
if cerr != nil {
return errors.InternalServerError("go.micro.client", cerr.Error())
}
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode)) return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
} }
if log.V(logger.DebugLevel) { if cerr := cf.Unmarshal(buf, rerr); cerr != nil {
buf, rerr := io.ReadAll(hrsp.Body) return errors.InternalServerError("go.micro.client", cerr.Error())
log.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
if err != nil {
return errors.InternalServerError("go.micro.client", rerr.Error())
}
hrsp.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
} }
if cerr := cf.ReadBody(hrsp.Body, err); cerr != nil { if err, ok = rerr.(error); !ok {
err = errors.InternalServerError("go.micro.client", cerr.Error()) err = &Error{rerr}
} }
} }

59
util.go
View File

@ -1,11 +1,9 @@
package http package http
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
@ -247,45 +245,48 @@ func newTemplate(path string) ([]string, error) {
func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp interface{}, opts client.CallOptions) error { func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp interface{}, opts client.CallOptions) error {
var err error var err error
var buf []byte
// fast path return
if hrsp.StatusCode == http.StatusNoContent {
return nil
}
select { select {
case <-ctx.Done(): case <-ctx.Done():
err = ctx.Err() err = ctx.Err()
default: default:
// fast path return
if hrsp.StatusCode == http.StatusNoContent {
return nil
}
ct := DefaultContentType ct := DefaultContentType
if htype := hrsp.Header.Get("Content-Type"); htype != "" { if htype := hrsp.Header.Get("Content-Type"); htype != "" {
ct = htype ct = htype
} }
cf, cerr := h.newCodec(ct) if hrsp.Body != nil {
if hrsp.StatusCode >= 400 && cerr != nil { buf, err = io.ReadAll(hrsp.Body)
var buf []byte if err != nil {
if hrsp.Body != nil { if h.opts.Logger.V(logger.ErrorLevel) {
buf, err = io.ReadAll(hrsp.Body)
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
h.opts.Logger.Errorf(ctx, "failed to read body: %v", err) h.opts.Logger.Errorf(ctx, "failed to read body: %v", err)
} }
return errors.InternalServerError("go.micro.client", string(buf))
} }
}
cf, cerr := h.newCodec(ct)
if cerr != nil {
if h.opts.Logger.V(logger.DebugLevel) { if h.opts.Logger.V(logger.DebugLevel) {
h.opts.Logger.Debugf(ctx, "response %s with %v", buf, hrsp.Header) h.opts.Logger.Debugf(ctx, "response with %v unknown content-type %s", hrsp.Header, ct, buf)
}
// response like text/plain or something else, return original error
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
} else if cerr != nil {
if h.opts.Logger.V(logger.DebugLevel) {
h.opts.Logger.Debugf(ctx, "response with %v unknown content-type", hrsp.Header, ct)
} }
return errors.InternalServerError("go.micro.client", cerr.Error()) return errors.InternalServerError("go.micro.client", cerr.Error())
} }
if h.opts.Logger.V(logger.DebugLevel) {
h.opts.Logger.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
}
// succeseful response // succeseful response
if hrsp.StatusCode < 400 { if hrsp.StatusCode < 400 {
if err = cf.ReadBody(hrsp.Body, rsp); err != nil { if err = cf.Unmarshal(buf, rsp); err != nil {
return errors.InternalServerError("go.micro.client", err.Error()) return errors.InternalServerError("go.micro.client", err.Error())
} }
return nil return nil
@ -302,26 +303,10 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte
} }
if !ok || rerr == nil { if !ok || rerr == nil {
buf, rerr := io.ReadAll(hrsp.Body)
if h.opts.Logger.V(logger.DebugLevel) {
h.opts.Logger.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
}
if rerr != nil {
return errors.InternalServerError("go.micro.client", rerr.Error())
}
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode)) return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
} }
if h.opts.Logger.V(logger.DebugLevel) { if cerr := cf.Unmarshal(buf, rerr); cerr != nil {
buf, rerr := io.ReadAll(hrsp.Body)
h.opts.Logger.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
if err != nil {
return errors.InternalServerError("go.micro.client", rerr.Error())
}
hrsp.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
}
if cerr := cf.ReadBody(hrsp.Body, rerr); cerr != nil {
return errors.InternalServerError("go.micro.client", cerr.Error()) return errors.InternalServerError("go.micro.client", cerr.Error())
} }