errLogValues inspects err and returns the status code to use, the error log message, and any extra fields. If err is a HandlerError, the returned values will have richer information.
(err error)
| 204 | // If err is a HandlerError, the returned values will |
| 205 | // have richer information. |
| 206 | func errLogValues(err error) (status int, msg string, fields func() []zapcore.Field) { |
| 207 | var handlerErr HandlerError |
| 208 | if errors.As(err, &handlerErr) { |
| 209 | status = handlerErr.StatusCode |
| 210 | if handlerErr.Err == nil { |
| 211 | msg = err.Error() |
| 212 | } else { |
| 213 | msg = handlerErr.Err.Error() |
| 214 | } |
| 215 | fields = func() []zapcore.Field { |
| 216 | return []zapcore.Field{ |
| 217 | zap.Int("status", handlerErr.StatusCode), |
| 218 | zap.String("err_id", handlerErr.ID), |
| 219 | zap.String("err_trace", handlerErr.Trace), |
| 220 | } |
| 221 | } |
| 222 | return status, msg, fields |
| 223 | } |
| 224 | fields = func() []zapcore.Field { |
| 225 | return []zapcore.Field{ |
| 226 | zap.Error(err), |
| 227 | } |
| 228 | } |
| 229 | status = http.StatusInternalServerError |
| 230 | msg = err.Error() |
| 231 | return status, msg, fields |
| 232 | } |
| 233 | |
| 234 | // ExtraLogFields is a list of extra fields to log with every request. |
| 235 | type ExtraLogFields struct { |