Errorf writes formatted error message to w and to logger.
(w http.ResponseWriter, r *http.Request, format string, args ...any)
| 652 | |
| 653 | // Errorf writes formatted error message to w and to logger. |
| 654 | func Errorf(w http.ResponseWriter, r *http.Request, format string, args ...any) { |
| 655 | errStr := fmt.Sprintf(format, args...) |
| 656 | remoteAddr := GetQuotedRemoteAddr(r) |
| 657 | requestURI := GetRequestURI(r) |
| 658 | errStr = fmt.Sprintf("remoteAddr: %s; requestURI: %s; %s", remoteAddr, requestURI, errStr) |
| 659 | logger.WarnfSkipframes(1, "%s", errStr) |
| 660 | |
| 661 | // Extract statusCode from args |
| 662 | statusCode := http.StatusBadRequest |
| 663 | var esc *ErrorWithStatusCode |
| 664 | for _, arg := range args { |
| 665 | if err, ok := arg.(error); ok && errors.As(err, &esc) { |
| 666 | statusCode = esc.StatusCode |
| 667 | break |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | if rwa, ok := w.(*responseWriterWithAbort); ok && rwa.sentHeaders { |
| 672 | // HTTP status code has been already sent to client, so it cannot be sent again. |
| 673 | // Just write errStr to the response and abort the client connection, so the client could notice the error. |
| 674 | fmt.Fprintf(w, "\n%s\n", errStr) |
| 675 | rwa.abort() |
| 676 | return |
| 677 | } |
| 678 | http.Error(w, errStr, statusCode) |
| 679 | } |
| 680 | |
| 681 | // ErrorWithStatusCode is error with HTTP status code. |
| 682 | // |
no test coverage detected
searching dependent graphs…