Skip to content

Commit 0d0d266

Browse files
committed
feat: hide textual errors on internal server errors
1 parent 2939c05 commit 0d0d266

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
lines changed

internal/api/common/errors.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package common
33
import (
44
"errors"
55
"github.com/formancehq/go-libs/v2/api"
6+
"github.com/formancehq/go-libs/v2/logging"
67
"github.com/formancehq/go-libs/v2/platform/postgres"
8+
"go.opentelemetry.io/otel/trace"
79
"net/http"
810
)
911

@@ -31,6 +33,15 @@ func HandleCommonErrors(w http.ResponseWriter, r *http.Request, err error) {
3133
case errors.Is(err, postgres.ErrTooManyClient{}):
3234
api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err)
3335
default:
34-
api.InternalServerError(w, r, err)
36+
InternalServerError(w, r, err)
3537
}
3638
}
39+
40+
func InternalServerError(w http.ResponseWriter, r *http.Request, err error) {
41+
span := trace.SpanFromContext(r.Context())
42+
if span != nil {
43+
span.RecordError(err)
44+
}
45+
logging.FromContext(r.Context()).Error(err)
46+
api.WriteErrorResponse(w, http.StatusInternalServerError, api.ErrorInternal, errors.New("Internal error. Consult logs/traces to have more details."))
47+
}

internal/api/common/middleware_resolver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func LedgerMiddleware(
4040
case postgres.IsNotFoundError(err):
4141
api.WriteErrorResponse(w, http.StatusNotFound, "LEDGER_NOT_FOUND", err)
4242
default:
43-
api.InternalServerError(w, r, err)
43+
InternalServerError(w, r, err)
4444
}
4545
return
4646
}
@@ -65,7 +65,7 @@ func LedgerMiddleware(
6565
if !excluded {
6666
isUpToDate, err := l.IsDatabaseUpToDate(ctx)
6767
if err != nil {
68-
api.InternalServerError(w, r, err)
68+
InternalServerError(w, r, err)
6969
return
7070
}
7171
if !isUpToDate {

internal/api/v1/middleware_auto_create_ledger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func autoCreateMiddleware(backend system.Controller, tracer trace.Tracer) func(h
2626
ledgerName := chi.URLParam(r, "ledger")
2727
if _, err := backend.GetLedger(ctx, ledgerName); err != nil {
2828
if !postgres.IsNotFoundError(err) {
29-
api.InternalServerError(w, r, err)
29+
common.InternalServerError(w, r, err)
3030
return
3131
}
3232

@@ -37,7 +37,7 @@ func autoCreateMiddleware(backend system.Controller, tracer trace.Tracer) func(h
3737
case errors.Is(err, ledger.ErrInvalidLedgerName{}):
3838
api.BadRequest(w, common.ErrValidation, err)
3939
default:
40-
api.InternalServerError(w, r, err)
40+
common.InternalServerError(w, r, err)
4141
}
4242
return
4343
}

internal/api/v2/controllers_bulk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func bulkHandler(bulkerFactory bulking.BulkerFactory, bulkHandlerFactories map[s
4343
},
4444
)
4545
if err != nil {
46-
api.InternalServerError(w, r, err)
46+
common.InternalServerError(w, r, err)
4747
return
4848
}
4949

internal/api/v2/controllers_ledgers_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func createLedger(systemController system.Controller) http.HandlerFunc {
2020
configuration := ledger.Configuration{}
2121
data, err := io.ReadAll(r.Body)
2222
if err != nil && !errors.Is(err, io.EOF) {
23-
api.InternalServerError(w, r, err)
23+
common.InternalServerError(w, r, err)
2424
return
2525
}
2626

internal/api/v2/controllers_logs_import.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ func importLogs(w http.ResponseWriter, r *http.Request) {
3636
close(stream)
3737
break
3838
} else {
39-
api.InternalServerError(w, r, err)
39+
common.InternalServerError(w, r, err)
4040
return
4141
}
4242
}
4343
select {
4444
case stream <- l:
4545
case <-r.Context().Done():
46-
api.InternalServerError(w, r, r.Context().Err())
46+
common.InternalServerError(w, r, r.Context().Err())
4747
return
4848
case err := <-errChan:
4949
handleError(err)
@@ -57,7 +57,7 @@ func importLogs(w http.ResponseWriter, r *http.Request) {
5757
return
5858
}
5959
case <-r.Context().Done():
60-
api.InternalServerError(w, r, r.Context().Err())
60+
common.InternalServerError(w, r, r.Context().Err())
6161
return
6262
}
6363

0 commit comments

Comments
 (0)