MCPcopy
hub / github.com/caddyserver/caddy / WithError

Method WithError

modules/caddyhttp/server.go:778–800  ·  view source on GitHub ↗

WithError makes a shallow copy of r to add the error to its context, and sets placeholders on the request's replacer related to err. It returns the modified request which has the error information in its context and replacer. It overwrites any existing error values that are stored.

(r *http.Request, err error)

Source from the content-addressed store, hash-verified

776// the error information in its context and replacer. It
777// overwrites any existing error values that are stored.
778func (*HTTPErrorConfig) WithError(r *http.Request, err error) *http.Request {
779 // add the raw error value to the request context
780 // so it can be accessed by error handlers
781 c := context.WithValue(r.Context(), ErrorCtxKey, err)
782 r = r.WithContext(c)
783
784 // add error values to the replacer
785 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
786 repl.Set("http.error", err)
787 if handlerErr, ok := err.(HandlerError); ok {
788 repl.Set("http.error.status_code", handlerErr.StatusCode)
789 repl.Set("http.error.status_text", http.StatusText(handlerErr.StatusCode))
790 repl.Set("http.error.id", handlerErr.ID)
791 repl.Set("http.error.trace", handlerErr.Trace)
792 if handlerErr.Err != nil {
793 repl.Set("http.error.message", handlerErr.Err.Error())
794 } else {
795 repl.Set("http.error.message", http.StatusText(handlerErr.StatusCode))
796 }
797 }
798
799 return r
800}
801
802// shouldLogRequest returns true if this request should be logged.
803func (s *Server) shouldLogRequest(r *http.Request) bool {

Callers 2

ServeHTTPMethod · 0.80
ServeHTTPMethod · 0.80

Calls 4

WithValueMethod · 0.80
ValueMethod · 0.45
SetMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected