From 949622ed93f082c436be79fd2e368661ea1c6c61 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:58:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=20=D0=BF=D0=BE=D0=BA=D0=B0=D0=B7?= =?UTF-8?q?=D1=8B=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD?= =?UTF-8?q?=D1=82=D1=83=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B5=D0=BA=D1=81=D1=82?= =?UTF-8?q?=20=D0=BE=D0=B1=D1=91=D1=80=D1=82=D0=BA=D0=B8=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/platform/httpx/httpx_test.go | 6 ++++-- internal/platform/httpx/respond.go | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/internal/platform/httpx/httpx_test.go b/internal/platform/httpx/httpx_test.go index 1bd0d07b..a745d5a9 100644 --- a/internal/platform/httpx/httpx_test.go +++ b/internal/platform/httpx/httpx_test.go @@ -319,8 +319,10 @@ func TestUnavailableCauseIsLoggedButNotReturned(t *testing.T) { }) 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()) + // Neither the cause nor the wrapping context of internal operations + // ("select user: ...") may reach the client. + if eb.Error.Message != "database unavailable" { + t.Fatalf("client message = %q, want only the application message", eb.Error.Message) } 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()) diff --git a/internal/platform/httpx/respond.go b/internal/platform/httpx/respond.go index c89e9712..30f49354 100644 --- a/internal/platform/httpx/respond.go +++ b/internal/platform/httpx/respond.go @@ -53,11 +53,17 @@ 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. 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. +// so internal details never reach the client. An application error shows the +// client only its own message, never the context it was wrapped in on the +// way up; when it is answered with a 5xx (a dependency outage) the full +// chain and the cause go to the log for the operator. func Error(c *gin.Context, err error) { - mapped, known := kinds[apperr.KindOf(err)] + appErr, isApp := errors.AsType[*apperr.Error](err) + var mapped statusCode + known := false + if isApp { + mapped, known = kinds[appErr.Kind] + } if !known { logger(c).Error("request failed", "request_id", RequestID(c), "error", err.Error()) Fail(c, http.StatusInternalServerError, "internal_error", "internal error") @@ -65,12 +71,12 @@ func Error(c *gin.Context, err error) { } 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()) + if appErr.Cause != nil { + attrs = append(attrs, "cause", appErr.Cause.Error()) } logger(c).Error("request failed", attrs...) } - Fail(c, mapped.status, mapped.code, err.Error()) + Fail(c, mapped.status, mapped.code, appErr.Message) } // Timestamp renders RFC 3339 UTC with a fixed millisecond width: Python 3.9