Пример #1
0
func (er *errorRewritingRoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error) {
	res, err := er.orig.RoundTrip(req)
	if err != nil {
		var htmlerr []byte

		// If the request has an 'Accept' header preferring HTML, or
		// doesn't have that header at all, render the error page.
		switch req.Header.Get("Accept") {
		case "text/html":
			fallthrough
		case "application/xhtml+xml":
			fallthrough
		case "":
			// It is likely we will have lots of different errors to handle but for now
			// we will only return a ErrorAccessingPage error.  This prevents the user
			// from getting just a blank screen.
			htmlerr, err = status.ErrorAccessingPage(req.Host, err)
			if err != nil {
				log.Debugf("Got error while generating status page: %q", err)
			}
		default:
			// We know for sure that the requested resource is not HTML page,
			// wrap the error message in http content, or http.ReverseProxy
			// will response 500 Internal Server Error instead.
			htmlerr = []byte(err.Error())
		}

		res = &http.Response{
			Body: ioutil.NopCloser(bytes.NewBuffer(htmlerr)),
		}
		res.StatusCode = http.StatusServiceUnavailable
		return res, nil
	}
	return res, err
}
Пример #2
0
func (er *errorRewritingRoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error) {
	res, err := er.orig.RoundTrip(req)
	if err != nil {

		// It is likely we will have lots of different errors to handle but for now
		// we will only return a ErrorAccessingPage error.  This prevents the user
		// from getting just a blank screen.
		htmlerr, err := status.ErrorAccessingPage(req.Host, err)

		if err != nil {
			log.Debugf("Got error while generating status page: %q", err)
		}

		res = &http.Response{
			Body: ioutil.NopCloser(bytes.NewBuffer(htmlerr)),
		}

		res.StatusCode = http.StatusServiceUnavailable
		return res, nil
	}
	return res, err
}