Skip to content

Commit 8072096

Browse files
committed
feat: only display panics when in debug mode
1 parent 06d5ff8 commit 8072096

File tree

4 files changed

+33
-44
lines changed

4 files changed

+33
-44
lines changed

Justfile

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ default:
44
@just --list
55

66
pre-commit: generate tidy lint export-docs-events openapi generate-client
7+
pc: pre-commit
78

89
lint:
910
@golangci-lint run --fix --build-tags it --timeout 5m

internal/api/router.go

+31-21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package api
22

33
import (
4+
"fmt"
45
"github.com/formancehq/go-libs/v2/api"
56
"github.com/formancehq/go-libs/v2/bun/bunpaginate"
7+
"github.com/formancehq/go-libs/v2/service"
68
"github.com/formancehq/ledger/internal/api/bulking"
79
"github.com/formancehq/ledger/internal/controller/system"
8-
"go.opentelemetry.io/otel/attribute"
910
"go.opentelemetry.io/otel/trace"
1011
nooptracer "go.opentelemetry.io/otel/trace/noop"
1112
"net/http"
@@ -36,37 +37,47 @@ func NewRouter(
3637

3738
mux := chi.NewRouter()
3839
mux.Use(
39-
middleware.Recoverer,
4040
cors.New(cors.Options{
4141
AllowOriginFunc: func(r *http.Request, origin string) bool {
4242
return true
4343
},
4444
AllowCredentials: true,
4545
}).Handler,
4646
common.LogID(),
47-
)
48-
49-
commonMiddlewares := []func(http.Handler) http.Handler{
5047
middleware.RequestLogger(api.NewLogFormatter()),
51-
}
52-
53-
if debug {
54-
commonMiddlewares = append(commonMiddlewares, func(handler http.Handler) http.Handler {
55-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
56-
trace.SpanFromContext(r.Context()).
57-
SetAttributes(attribute.String("raw-query", r.URL.RawQuery))
58-
59-
handler.ServeHTTP(w, r)
60-
})
61-
})
62-
}
48+
service.OTLPMiddleware("ledger", debug),
49+
func(next http.Handler) http.Handler {
50+
fn := func(w http.ResponseWriter, r *http.Request) {
51+
defer func() {
52+
if rvr := recover(); rvr != nil {
53+
if rvr == http.ErrAbortHandler {
54+
// we don't recover http.ErrAbortHandler so the response
55+
// to the client is aborted, this should not be logged
56+
panic(rvr)
57+
}
58+
59+
if debug {
60+
middleware.PrintPrettyStack(rvr)
61+
}
62+
63+
trace.SpanFromContext(r.Context()).RecordError(fmt.Errorf("%s", rvr))
64+
65+
w.WriteHeader(http.StatusInternalServerError)
66+
}
67+
}()
68+
69+
next.ServeHTTP(w, r)
70+
}
71+
72+
return http.HandlerFunc(fn)
73+
},
74+
)
6375

6476
v2Router := v2.NewRouter(
6577
systemController,
6678
authenticator,
6779
debug,
6880
v2.WithTracer(routerOptions.tracer),
69-
v2.WithMiddlewares(commonMiddlewares...),
7081
v2.WithBulkerFactory(routerOptions.bulkerFactory),
7182
v2.WithBulkHandlerFactories(map[string]bulking.HandlerFactory{
7283
"application/json": bulking.NewJSONBulkHandlerFactory(routerOptions.bulkMaxSize),
@@ -84,15 +95,14 @@ func NewRouter(
8495
version,
8596
debug,
8697
v1.WithTracer(routerOptions.tracer),
87-
v1.WithMiddlewares(commonMiddlewares...),
8898
))
8999

90100
return mux
91101
}
92102

93103
type routerOptions struct {
94-
tracer trace.Tracer
95-
bulkMaxSize int
104+
tracer trace.Tracer
105+
bulkMaxSize int
96106
bulkerFactory bulking.BulkerFactory
97107
paginationConfig common.PaginationConfig
98108
}

internal/api/v1/routes.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77

88
"github.com/formancehq/ledger/internal/controller/system"
99

10-
"github.com/formancehq/go-libs/v2/service"
11-
1210
"github.com/formancehq/go-libs/v2/auth"
1311
"github.com/formancehq/ledger/internal/api/common"
1412
"github.com/go-chi/chi/v5"
@@ -32,9 +30,7 @@ func NewRouter(
3230
router.Get("/_info", getInfo(systemController, version))
3331

3432
router.Group(func(router chi.Router) {
35-
router.Use(routerOptions.middlewares...)
3633
router.Use(auth.Middleware(authenticator))
37-
router.Use(service.OTLPMiddleware("ledger", debug))
3834

3935
router.Route("/{ledger}", func(router chi.Router) {
4036
router.Use(func(handler http.Handler) http.Handler {
@@ -82,8 +78,7 @@ func NewRouter(
8278
}
8379

8480
type routerOptions struct {
85-
tracer trace.Tracer
86-
middlewares []func(handler http.Handler) http.Handler
81+
tracer trace.Tracer
8782
}
8883

8984
type RouterOption func(ro *routerOptions)
@@ -94,12 +89,6 @@ func WithTracer(tracer trace.Tracer) RouterOption {
9489
}
9590
}
9691

97-
func WithMiddlewares(handlers ...func(http.Handler) http.Handler) RouterOption {
98-
return func(ro *routerOptions) {
99-
ro.middlewares = append(ro.middlewares, handlers...)
100-
}
101-
}
102-
10392
var defaultRouterOptions = []RouterOption{
10493
WithTracer(nooptracer.Tracer{}),
10594
}

internal/api/v2/routes.go

-11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"go.opentelemetry.io/otel/attribute"
1212
"go.opentelemetry.io/otel/trace"
1313

14-
"github.com/formancehq/go-libs/v2/service"
15-
1614
"github.com/formancehq/go-libs/v2/auth"
1715
"github.com/formancehq/ledger/internal/api/common"
1816
"github.com/go-chi/chi/v5"
@@ -32,9 +30,7 @@ func NewRouter(
3230
router := chi.NewMux()
3331

3432
router.Group(func(router chi.Router) {
35-
router.Use(routerOptions.middlewares...)
3633
router.Use(auth.Middleware(authenticator))
37-
router.Use(service.OTLPMiddleware("ledger", debug))
3834

3935
router.Get("/", listLedgers(systemController, routerOptions.paginationConfig))
4036
router.Route("/{ledger}", func(router chi.Router) {
@@ -96,7 +92,6 @@ func NewRouter(
9692

9793
type routerOptions struct {
9894
tracer trace.Tracer
99-
middlewares []func(http.Handler) http.Handler
10095
bulkerFactory bulking.BulkerFactory
10196
bulkHandlerFactories map[string]bulking.HandlerFactory
10297
paginationConfig common.PaginationConfig
@@ -110,12 +105,6 @@ func WithTracer(tracer trace.Tracer) RouterOption {
110105
}
111106
}
112107

113-
func WithMiddlewares(middlewares ...func(http.Handler) http.Handler) RouterOption {
114-
return func(ro *routerOptions) {
115-
ro.middlewares = append(ro.middlewares, middlewares...)
116-
}
117-
}
118-
119108
func WithBulkHandlerFactories(bulkHandlerFactories map[string]bulking.HandlerFactory) RouterOption {
120109
return func(ro *routerOptions) {
121110
ro.bulkHandlerFactories = bulkHandlerFactories

0 commit comments

Comments
 (0)