From 0ea848ed8a239efdab081921e66de6321f3abb00 Mon Sep 17 00:00:00 2001 From: Bryan Frimin Date: Fri, 16 May 2025 20:40:16 -0700 Subject: [PATCH] Log graphql error Signed-off-by: Bryan Frimin --- pkg/probod/probod.go | 1 + pkg/server/api/api.go | 14 +++++++++++- pkg/server/api/console/v1/resolver.go | 33 ++++++++++++--------------- pkg/server/server.go | 3 +++ 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/pkg/probod/probod.go b/pkg/probod/probod.go index ba26a8022..16e088f88 100644 --- a/pkg/probod/probod.go +++ b/pkg/probod/probod.go @@ -217,6 +217,7 @@ func (impl *Implm) Run( Usrmgr: usrmgrService, ConnectorRegistry: defaultConnectorRegistry, SafeRedirect: &saferedirect.SafeRedirect{AllowedHost: impl.cfg.Hostname}, + Logger: l.Named("http.server"), Auth: console_v1.AuthConfig{ CookieName: impl.cfg.Auth.Cookie.Name, CookieDomain: impl.cfg.Auth.Cookie.Domain, diff --git a/pkg/server/api/api.go b/pkg/server/api/api.go index 6aadc2940..2c4377cb1 100644 --- a/pkg/server/api/api.go +++ b/pkg/server/api/api.go @@ -26,6 +26,7 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/cors" "go.gearno.de/kit/httpserver" + "go.gearno.de/kit/log" ) type ( @@ -36,6 +37,7 @@ type ( Auth console_v1.AuthConfig ConnectorRegistry *connector.ConnectorRegistry SafeRedirect *saferedirect.SafeRedirect + Logger *log.Logger } Server struct { @@ -105,7 +107,17 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { router.Use(cors.Handler(corsOpts)) // Mount the console API with authentication - router.Mount("/console/v1", console_v1.NewMux(s.cfg.Probo, s.cfg.Usrmgr, s.cfg.Auth, s.cfg.ConnectorRegistry, s.cfg.SafeRedirect)) + router.Mount( + "/console/v1", + console_v1.NewMux( + s.cfg.Logger.Named("console.v1"), + s.cfg.Probo, + s.cfg.Usrmgr, + s.cfg.Auth, + s.cfg.ConnectorRegistry, + s.cfg.SafeRedirect, + ), + ) router.ServeHTTP(w, r) } diff --git a/pkg/server/api/console/v1/resolver.go b/pkg/server/api/console/v1/resolver.go index 118c8264d..782894c3d 100644 --- a/pkg/server/api/console/v1/resolver.go +++ b/pkg/server/api/console/v1/resolver.go @@ -41,6 +41,7 @@ import ( "github.com/getprobo/probo/pkg/usrmgr" "github.com/go-chi/chi/v5" "github.com/vektah/gqlparser/v2/gqlerror" + "go.gearno.de/kit/log" ) type ( @@ -77,7 +78,14 @@ func UserFromContext(ctx context.Context) *coredata.User { return user } -func NewMux(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg AuthConfig, connectorRegistry *connector.ConnectorRegistry, safeRedirect *saferedirect.SafeRedirect) *chi.Mux { +func NewMux( + logger *log.Logger, + proboSvc *probo.Service, + usrmgrSvc *usrmgr.Service, + authCfg AuthConfig, + connectorRegistry *connector.ConnectorRegistry, + safeRedirect *saferedirect.SafeRedirect, +) *chi.Mux { r := chi.NewMux() r.Get( @@ -197,12 +205,12 @@ func NewMux(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg AuthConf })) r.Get("/", playground.Handler("GraphQL", "/api/console/v1/query")) - r.Post("/query", graphqlHandler(proboSvc, usrmgrSvc, authCfg)) + r.Post("/query", graphqlHandler(logger, proboSvc, usrmgrSvc, authCfg)) return r } -func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg AuthConfig) http.HandlerFunc { +func graphqlHandler(logger *log.Logger, proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg AuthConfig) http.HandlerFunc { var mb int64 = 1 << 20 es := schema.NewExecutableSchema( @@ -225,8 +233,8 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg srv.Use(extension.Introspection{}) srv.Use(tracingExtension{}) srv.SetRecoverFunc(func(ctx context.Context, err any) error { - panicValue := ctx.Value(panicValueContextKey).(*any) - *panicValue = err + logger.Error("resolver panic", log.Any("error", err)) + return fmt.Errorf("resolver panic: %v", err) }) @@ -253,20 +261,7 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg }, ) - return WithSession(usrmgrSvc, authCfg, func(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - - // Hack to capture the panic value, because gqlgen execute resolver in a different goroutine. - // And I want use the go.gearno.de/kit/httpserver built in panic recovery. - var panicValue any - ctx = context.WithValue(ctx, panicValueContextKey, &panicValue) - - srv.ServeHTTP(w, r.WithContext(ctx)) - - if panicValue != nil { - panic(panicValue) - } - }) + return WithSession(usrmgrSvc, authCfg, srv.ServeHTTP) } func WithSession(usrmgrSvc *usrmgr.Service, authCfg AuthConfig, next http.HandlerFunc) http.HandlerFunc { diff --git a/pkg/server/server.go b/pkg/server/server.go index b4b341862..dba756728 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -27,6 +27,7 @@ import ( "github.com/getprobo/probo/pkg/server/web" "github.com/getprobo/probo/pkg/usrmgr" "github.com/go-chi/chi/v5" + "go.gearno.de/kit/log" ) // Config holds the configuration for the server @@ -37,6 +38,7 @@ type Config struct { Auth console_v1.AuthConfig ConnectorRegistry *connector.ConnectorRegistry SafeRedirect *saferedirect.SafeRedirect + Logger *log.Logger } // Server represents the main server that handles both API and frontend requests @@ -56,6 +58,7 @@ func NewServer(cfg Config) (*Server, error) { Auth: cfg.Auth, ConnectorRegistry: cfg.ConnectorRegistry, SafeRedirect: cfg.SafeRedirect, + Logger: cfg.Logger.Named("api"), } apiServer, err := api.NewServer(apiCfg) if err != nil {