in proxy/protocol/http/sidecar.go [236:298]
func handleRequest(w http.ResponseWriter, inv *invocation.Invocation, ir *invocation.Response) (*http.Response, error) {
if ir != nil {
if ir.Err != nil {
//handler only mesher errors, ignore http response err
switch ir.Err.(type) {
case hystrix.FallbackNullError:
handleErrorResponse(inv, w, http.StatusOK, nil)
case loadbalancer.LBError:
handleErrorResponse(inv, w, http.StatusBadGateway, ir.Err)
case hystrix.CircuitError:
handleErrorResponse(inv, w, http.StatusServiceUnavailable, ir.Err)
case fault.Fault:
handleErrorResponse(inv, w, ir.Status, ir.Err)
default: //for other error, check response and response body, if there is body, just transparent response
resp, ok := inv.Reply.(*http.Response)
if ok { // return raw transport error
if resp != nil {
if resp.Body == nil {
//resp.Resp can be nil, for example network error, must handle it
handleErrorResponse(inv, w, http.StatusBadGateway, ir.Err)
return nil, ir.Err
}
copyChassisResp2HttpResp(w, resp)
RecordStatus(inv, resp.StatusCode)
} else {
// unknown error, resp is nil, e.g. connection refused
handleErrorResponse(inv, w, http.StatusBadGateway, ir.Err)
}
} else { // unknown err in handler chain
handleErrorResponse(inv, w, http.StatusInternalServerError, ir.Err)
}
}
return nil, ir.Err
}
if inv.Endpoint == "" {
handleErrorResponse(inv, w, http.StatusBadGateway, protocol.ErrUnknown)
return nil, protocol.ErrUnknown
}
if ir.Result == nil {
if ir.Err != nil {
handleErrorResponse(inv, w, http.StatusBadGateway, ir.Err)
return nil, ir.Err
}
handleErrorResponse(inv, w, http.StatusBadGateway, ErrNilResponse)
return nil, protocol.ErrUnknown
}
resp, ok := ir.Result.(*http.Response)
if !ok {
err := errors.New("invocationResponse result is not type *rest.Response")
handleErrorResponse(inv, w, http.StatusBadGateway, err)
return nil, err
}
//transparent proxy
copyChassisResp2HttpResp(w, resp)
return resp, nil
} else {
handleErrorResponse(inv, w, http.StatusBadGateway, protocol.ErrUnExpectedHandlerChainResponse)
return nil, protocol.ErrUnExpectedHandlerChainResponse
}
}