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)
| 776 | // the error information in its context and replacer. It |
| 777 | // overwrites any existing error values that are stored. |
| 778 | func (*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. |
| 803 | func (s *Server) shouldLogRequest(r *http.Request) bool { |