ErrorAndLog writes a formatted HTTP error response and logs it at [slog.LevelError] level. l, r, and w must not be nil.
( ctx context.Context, l *slog.Logger, r *http.Request, w http.ResponseWriter, code int, format string, args ...any, )
| 29 | // ErrorAndLog writes a formatted HTTP error response and logs it at |
| 30 | // [slog.LevelError] level. l, r, and w must not be nil. |
| 31 | func ErrorAndLog( |
| 32 | ctx context.Context, |
| 33 | l *slog.Logger, |
| 34 | r *http.Request, |
| 35 | w http.ResponseWriter, |
| 36 | code int, |
| 37 | format string, |
| 38 | args ...any, |
| 39 | ) { |
| 40 | text := fmt.Sprintf(format, args...) |
| 41 | l.WarnContext( |
| 42 | ctx, |
| 43 | "http error", |
| 44 | "host", r.Host, |
| 45 | "method", r.Method, |
| 46 | "raddr", r.RemoteAddr, |
| 47 | "request_uri", r.RequestURI, |
| 48 | "status", code, |
| 49 | slogutil.KeyError, text, |
| 50 | ) |
| 51 | |
| 52 | http.Error(w, text, code) |
| 53 | } |
| 54 | |
| 55 | // UserAgent returns the ID of the service as a User-Agent string. It can also |
| 56 | // be used as the value of the Server HTTP header. |
no test coverage detected
searching dependent graphs…