Skip to content

Commit

Permalink
validation message in backend log's message field (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Koch authored and Marcel Ludwig committed Dec 9, 2020
1 parent 8622993 commit f46c641
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions handler/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,20 @@ func (p *Proxy) roundtrip(rw http.ResponseWriter, req *http.Request) {
outreq.Header.Set("X-Forwarded-For", clientIP)
}

roundtripInfo := req.Context().Value(request.RoundtripInfo).(*logging.RoundtripInfo)

validationCtx, route, requestValidationInput, err := p.prepareRequestValidation(outreq)
if err != nil {
// this only happens if os.Getwd() fails
// TODO: use error template from parent endpoint>api>server
p.log.WithField("upstream request validation", err).Error()
roundtripInfo.Err = err
couperErr.DefaultJSON.ServeError(couperErr.UpstreamRequestValidationFailed).ServeHTTP(rw, req)
return
}
if requestValidationInput != nil {
if err := openapi3filter.ValidateRequest(validationCtx, requestValidationInput); err != nil {
// TODO: use error template from parent endpoint>api>server
p.log.WithField("upstream request validation", err).Error()
roundtripInfo.Err = err
if !p.options.OpenAPI.IgnoreRequestViolations {
couperErr.DefaultJSON.ServeError(couperErr.UpstreamRequestValidationFailed).ServeHTTP(rw, req)
return
Expand All @@ -300,8 +302,10 @@ func (p *Proxy) roundtrip(rw http.ResponseWriter, req *http.Request) {
}

res, err := p.getTransport(outreq.URL.Scheme, outreq.URL.Host, outreq.Host).RoundTrip(outreq)
roundtripInfo := req.Context().Value(request.RoundtripInfo).(*logging.RoundtripInfo)
roundtripInfo.BeReq, roundtripInfo.BeResp, roundtripInfo.Err = outreq, res, err
roundtripInfo.BeReq, roundtripInfo.BeResp = outreq, res
if err != nil {
roundtripInfo.Err = err
}
if err != nil {
p.srvOptions.APIErrTpl.ServeError(couperErr.APIConnect).ServeHTTP(rw, req)
return
Expand Down Expand Up @@ -332,14 +336,14 @@ func (p *Proxy) roundtrip(rw http.ResponseWriter, req *http.Request) {
if err != nil {
// this only happens if response body buffering fails
// TODO: use error template from parent endpoint>api>server
p.log.WithField("upstream response validation", err).Error()
roundtripInfo.Err = err
couperErr.DefaultJSON.ServeError(couperErr.UpstreamResponseBufferingFailed).ServeHTTP(rw, req)
return
}
if responseValidationInput != nil && route != nil {
if err := openapi3filter.ValidateResponse(validationCtx, responseValidationInput); err != nil {
// TODO: use error template from parent endpoint>api>server
p.log.WithField("upstream response validation", err).Error()
roundtripInfo.Err = err
if !p.options.OpenAPI.IgnoreResponseViolations {
couperErr.DefaultJSON.ServeError(couperErr.UpstreamResponseValidationFailed).ServeHTTP(rw, req)
return
Expand Down

0 comments on commit f46c641

Please sign in to comment.