Логировать причину ответов 5xx при отказе зависимостей
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -307,6 +308,25 @@ func TestWithUserWithoutIdentityIsAWiringError(t *testing.T) {
|
|||||||
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
|
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A 5xx tells the client only the safe message; the operator must still see
|
||||||
|
// why the dependency failed, so the cause goes to the log.
|
||||||
|
func TestUnavailableCauseIsLoggedButNotReturned(t *testing.T) {
|
||||||
|
logs := &bytes.Buffer{}
|
||||||
|
e := newEngine(t, logs)
|
||||||
|
e.GET("/down", func(c *gin.Context) {
|
||||||
|
cause := errors.New("dial tcp 10.0.0.7:5432: connect: connection refused")
|
||||||
|
httpx.Error(c, fmt.Errorf("select user: %w", apperr.Wrap(apperr.KindUnavailable, "database unavailable", cause)))
|
||||||
|
})
|
||||||
|
rec, eb := do(t, e, http.MethodGet, "/down", "", nil)
|
||||||
|
assertError(t, rec, eb, http.StatusServiceUnavailable, "unavailable")
|
||||||
|
if strings.Contains(rec.Body.String(), "connection refused") {
|
||||||
|
t.Fatalf("cause leaked to the client: %s", rec.Body.String())
|
||||||
|
}
|
||||||
|
if !strings.Contains(logs.String(), "connection refused") || !strings.Contains(logs.String(), eb.Error.RequestID) {
|
||||||
|
t.Fatalf("log must carry the cause and the request id, got:\n%s", logs.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Everything below Timeout sees one deadline, so waiting on a dependency
|
// Everything below Timeout sees one deadline, so waiting on a dependency
|
||||||
// ends with the request instead of holding the client.
|
// ends with the request instead of holding the client.
|
||||||
func TestTimeoutBoundsTheRequestContext(t *testing.T) {
|
func TestTimeoutBoundsTheRequestContext(t *testing.T) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package httpx
|
package httpx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"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
|
// 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,
|
// 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) {
|
func Error(c *gin.Context, err error) {
|
||||||
mapped, known := kinds[apperr.KindOf(err)]
|
mapped, known := kinds[apperr.KindOf(err)]
|
||||||
if !known {
|
if !known {
|
||||||
@@ -60,6 +63,13 @@ func Error(c *gin.Context, err error) {
|
|||||||
Fail(c, http.StatusInternalServerError, "internal_error", "internal error")
|
Fail(c, http.StatusInternalServerError, "internal_error", "internal error")
|
||||||
return
|
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())
|
Fail(c, mapped.status, mapped.code, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user