Skip to content

Commit

Permalink
feat: only display panics when in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Jan 13, 2025
1 parent 06d5ff8 commit c10e8f0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
1 change: 1 addition & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ default:
@just --list

pre-commit: generate tidy lint export-docs-events openapi generate-client
pc: pre-commit

lint:
@golangci-lint run --fix --build-tags it --timeout 5m
Expand Down
52 changes: 31 additions & 21 deletions internal/api/router.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package api

import (
"fmt"
"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/go-libs/v2/bun/bunpaginate"
"github.com/formancehq/go-libs/v2/service"
"github.com/formancehq/ledger/internal/api/bulking"
"github.com/formancehq/ledger/internal/controller/system"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
nooptracer "go.opentelemetry.io/otel/trace/noop"
"net/http"
Expand Down Expand Up @@ -36,37 +37,47 @@ func NewRouter(

mux := chi.NewRouter()
mux.Use(
middleware.Recoverer,
cors.New(cors.Options{
AllowOriginFunc: func(r *http.Request, origin string) bool {
return true
},
AllowCredentials: true,
}).Handler,
common.LogID(),
)

commonMiddlewares := []func(http.Handler) http.Handler{
middleware.RequestLogger(api.NewLogFormatter()),
}

if debug {
commonMiddlewares = append(commonMiddlewares, func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
trace.SpanFromContext(r.Context()).
SetAttributes(attribute.String("raw-query", r.URL.RawQuery))

handler.ServeHTTP(w, r)
})
})
}
service.OTLPMiddleware("ledger", debug),
func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil {
if rvr == http.ErrAbortHandler {
// we don't recover http.ErrAbortHandler so the response
// to the client is aborted, this should not be logged
panic(rvr)

Check warning on line 56 in internal/api/router.go

View check run for this annotation

Codecov / codecov/patch

internal/api/router.go#L53-L56

Added lines #L53 - L56 were not covered by tests
}

if debug {
middleware.PrintPrettyStack(rvr)
}

Check warning on line 61 in internal/api/router.go

View check run for this annotation

Codecov / codecov/patch

internal/api/router.go#L59-L61

Added lines #L59 - L61 were not covered by tests

trace.SpanFromContext(r.Context()).RecordError(fmt.Errorf("%s", rvr))

w.WriteHeader(http.StatusInternalServerError)

Check warning on line 65 in internal/api/router.go

View check run for this annotation

Codecov / codecov/patch

internal/api/router.go#L63-L65

Added lines #L63 - L65 were not covered by tests
}
}()

next.ServeHTTP(w, r)
}

return http.HandlerFunc(fn)
},
)

v2Router := v2.NewRouter(
systemController,
authenticator,
debug,
v2.WithTracer(routerOptions.tracer),
v2.WithMiddlewares(commonMiddlewares...),
v2.WithBulkerFactory(routerOptions.bulkerFactory),
v2.WithBulkHandlerFactories(map[string]bulking.HandlerFactory{
"application/json": bulking.NewJSONBulkHandlerFactory(routerOptions.bulkMaxSize),
Expand All @@ -84,15 +95,14 @@ func NewRouter(
version,
debug,
v1.WithTracer(routerOptions.tracer),
v1.WithMiddlewares(commonMiddlewares...),
))

return mux
}

type routerOptions struct {
tracer trace.Tracer
bulkMaxSize int
tracer trace.Tracer
bulkMaxSize int
bulkerFactory bulking.BulkerFactory
paginationConfig common.PaginationConfig
}
Expand Down
13 changes: 1 addition & 12 deletions internal/api/v1/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (

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

"github.com/formancehq/go-libs/v2/service"

"github.com/formancehq/go-libs/v2/auth"
"github.com/formancehq/ledger/internal/api/common"
"github.com/go-chi/chi/v5"
Expand All @@ -32,9 +30,7 @@ func NewRouter(
router.Get("/_info", getInfo(systemController, version))

router.Group(func(router chi.Router) {
router.Use(routerOptions.middlewares...)
router.Use(auth.Middleware(authenticator))
router.Use(service.OTLPMiddleware("ledger", debug))

router.Route("/{ledger}", func(router chi.Router) {
router.Use(func(handler http.Handler) http.Handler {
Expand Down Expand Up @@ -82,8 +78,7 @@ func NewRouter(
}

type routerOptions struct {
tracer trace.Tracer
middlewares []func(handler http.Handler) http.Handler
tracer trace.Tracer
}

type RouterOption func(ro *routerOptions)
Expand All @@ -94,12 +89,6 @@ func WithTracer(tracer trace.Tracer) RouterOption {
}
}

func WithMiddlewares(handlers ...func(http.Handler) http.Handler) RouterOption {
return func(ro *routerOptions) {
ro.middlewares = append(ro.middlewares, handlers...)
}
}

var defaultRouterOptions = []RouterOption{
WithTracer(nooptracer.Tracer{}),
}
11 changes: 0 additions & 11 deletions internal/api/v2/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

"github.com/formancehq/go-libs/v2/service"

"github.com/formancehq/go-libs/v2/auth"
"github.com/formancehq/ledger/internal/api/common"
"github.com/go-chi/chi/v5"
Expand All @@ -32,9 +30,7 @@ func NewRouter(
router := chi.NewMux()

router.Group(func(router chi.Router) {
router.Use(routerOptions.middlewares...)
router.Use(auth.Middleware(authenticator))
router.Use(service.OTLPMiddleware("ledger", debug))

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

type routerOptions struct {
tracer trace.Tracer
middlewares []func(http.Handler) http.Handler
bulkerFactory bulking.BulkerFactory
bulkHandlerFactories map[string]bulking.HandlerFactory
paginationConfig common.PaginationConfig
Expand All @@ -110,12 +105,6 @@ func WithTracer(tracer trace.Tracer) RouterOption {
}
}

func WithMiddlewares(middlewares ...func(http.Handler) http.Handler) RouterOption {
return func(ro *routerOptions) {
ro.middlewares = append(ro.middlewares, middlewares...)
}
}

func WithBulkHandlerFactories(bulkHandlerFactories map[string]bulking.HandlerFactory) RouterOption {
return func(ro *routerOptions) {
ro.bulkHandlerFactories = bulkHandlerFactories
Expand Down

0 comments on commit c10e8f0

Please sign in to comment.