Логировать причину ответов 5xx при отказе зависимостей
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -52,7 +53,9 @@ func Fail(c *gin.Context, status int, code, message string) {
|
||||
|
||||
// Error maps an application error to its status. Anything that is not an
|
||||
// application error is logged with its text and reported as an opaque 500,
|
||||
// so internal details never reach the client.
|
||||
// so internal details never reach the client. A known error answered with a
|
||||
// 5xx (a dependency outage) shows the client only its safe message, and its
|
||||
// cause goes to the log for the operator.
|
||||
func Error(c *gin.Context, err error) {
|
||||
mapped, known := kinds[apperr.KindOf(err)]
|
||||
if !known {
|
||||
@@ -60,6 +63,13 @@ func Error(c *gin.Context, err error) {
|
||||
Fail(c, http.StatusInternalServerError, "internal_error", "internal error")
|
||||
return
|
||||
}
|
||||
if mapped.status >= http.StatusInternalServerError {
|
||||
attrs := []any{"request_id", RequestID(c), "error", err.Error()}
|
||||
if e, ok := errors.AsType[*apperr.Error](err); ok && e.Cause != nil {
|
||||
attrs = append(attrs, "cause", e.Cause.Error())
|
||||
}
|
||||
logger(c).Error("request failed", attrs...)
|
||||
}
|
||||
Fail(c, mapped.status, mapped.code, err.Error())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user