Логировать причину ответов 5xx при отказе зависимостей

This commit is contained in:
2026-10-06 15:43:23 +03:00
parent 3001507ecb
commit 405fb07608
2 changed files with 31 additions and 1 deletions
+20
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
@@ -307,6 +308,25 @@ func TestWithUserWithoutIdentityIsAWiringError(t *testing.T) {
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
// ends with the request instead of holding the client.
func TestTimeoutBoundsTheRequestContext(t *testing.T) {