Skip to content

Commit

Permalink
fixed modify response marshalling failed for null response
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyaw Myint Thein committed Sep 22, 2021
1 parent ac677e1 commit dcb5fb4
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions twirp.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,24 @@ func NewConfiguredBackendFactory(l logging.Logger, ref func(*config.Backend) cli
if reqMod != nil {
err = reqMod.ModifyRequest(req)
if err != nil {
l.Error(err, "failed to modify request")
return nil, err
}
}

request.Body.Close()
resp, err := callService(ctx, req, twirpOpt, l)
request.Body.Close()
req.Body.Close()
if err != nil {
l.Warning("gRPC calling the next mw:", err.Error())
l.Warning("RPC calling the next mw:", err.Error())
return nil, err
}

err = modifyProxyResponse(respMod, req, resp)
if err != nil {
l.Error(err, "failed to modify response")
return resp, err
}

return resp, err
}
}
Expand Down Expand Up @@ -326,16 +327,19 @@ func modifyResponse(mod martian.ResponseModifier, resp *http.Response) error {
if resp.Body == nil {
resp.Body = ioutil.NopCloser(bytes.NewBufferString(""))
}

if resp.Header == nil {
resp.Header = http.Header{}
}

if resp.StatusCode == 0 {
resp.StatusCode = http.StatusOK
}

if mod == nil {
return nil
}

return mod.ModifyResponse(resp)
}

Expand All @@ -351,9 +355,11 @@ func modifyProxyResponse(mod martian.ResponseModifier, request *http.Request, re
StatusCode: resp.Metadata.StatusCode,
Header: resp.Metadata.Headers,
}

if mod == nil {
return nil
}

err = mod.ModifyResponse(&httpResponse)
if err != nil {
return err
Expand All @@ -364,6 +370,10 @@ func modifyProxyResponse(mod martian.ResponseModifier, request *http.Request, re
return err
}

if len(modifiedResponseBytes) <= 0 {
modifiedResponseBytes = []byte(`{}`)
}

var data map[string]interface{}
err = json.Unmarshal(modifiedResponseBytes, &data)
if err != nil {
Expand Down

0 comments on commit dcb5fb4

Please sign in to comment.