Skip to content

Commit 0ea848e

Browse files
committed
Log graphql error
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
1 parent 366fd9e commit 0ea848e

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

pkg/probod/probod.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ func (impl *Implm) Run(
217217
Usrmgr: usrmgrService,
218218
ConnectorRegistry: defaultConnectorRegistry,
219219
SafeRedirect: &saferedirect.SafeRedirect{AllowedHost: impl.cfg.Hostname},
220+
Logger: l.Named("http.server"),
220221
Auth: console_v1.AuthConfig{
221222
CookieName: impl.cfg.Auth.Cookie.Name,
222223
CookieDomain: impl.cfg.Auth.Cookie.Domain,

pkg/server/api/api.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/go-chi/chi/v5"
2727
"github.com/go-chi/cors"
2828
"go.gearno.de/kit/httpserver"
29+
"go.gearno.de/kit/log"
2930
)
3031

3132
type (
@@ -36,6 +37,7 @@ type (
3637
Auth console_v1.AuthConfig
3738
ConnectorRegistry *connector.ConnectorRegistry
3839
SafeRedirect *saferedirect.SafeRedirect
40+
Logger *log.Logger
3941
}
4042

4143
Server struct {
@@ -105,7 +107,17 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
105107
router.Use(cors.Handler(corsOpts))
106108

107109
// Mount the console API with authentication
108-
router.Mount("/console/v1", console_v1.NewMux(s.cfg.Probo, s.cfg.Usrmgr, s.cfg.Auth, s.cfg.ConnectorRegistry, s.cfg.SafeRedirect))
110+
router.Mount(
111+
"/console/v1",
112+
console_v1.NewMux(
113+
s.cfg.Logger.Named("console.v1"),
114+
s.cfg.Probo,
115+
s.cfg.Usrmgr,
116+
s.cfg.Auth,
117+
s.cfg.ConnectorRegistry,
118+
s.cfg.SafeRedirect,
119+
),
120+
)
109121

110122
router.ServeHTTP(w, r)
111123
}

pkg/server/api/console/v1/resolver.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/getprobo/probo/pkg/usrmgr"
4242
"github.com/go-chi/chi/v5"
4343
"github.com/vektah/gqlparser/v2/gqlerror"
44+
"go.gearno.de/kit/log"
4445
)
4546

4647
type (
@@ -77,7 +78,14 @@ func UserFromContext(ctx context.Context) *coredata.User {
7778
return user
7879
}
7980

80-
func NewMux(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg AuthConfig, connectorRegistry *connector.ConnectorRegistry, safeRedirect *saferedirect.SafeRedirect) *chi.Mux {
81+
func NewMux(
82+
logger *log.Logger,
83+
proboSvc *probo.Service,
84+
usrmgrSvc *usrmgr.Service,
85+
authCfg AuthConfig,
86+
connectorRegistry *connector.ConnectorRegistry,
87+
safeRedirect *saferedirect.SafeRedirect,
88+
) *chi.Mux {
8189
r := chi.NewMux()
8290

8391
r.Get(
@@ -197,12 +205,12 @@ func NewMux(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg AuthConf
197205
}))
198206

199207
r.Get("/", playground.Handler("GraphQL", "/api/console/v1/query"))
200-
r.Post("/query", graphqlHandler(proboSvc, usrmgrSvc, authCfg))
208+
r.Post("/query", graphqlHandler(logger, proboSvc, usrmgrSvc, authCfg))
201209

202210
return r
203211
}
204212

205-
func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg AuthConfig) http.HandlerFunc {
213+
func graphqlHandler(logger *log.Logger, proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg AuthConfig) http.HandlerFunc {
206214
var mb int64 = 1 << 20
207215

208216
es := schema.NewExecutableSchema(
@@ -225,8 +233,8 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg
225233
srv.Use(extension.Introspection{})
226234
srv.Use(tracingExtension{})
227235
srv.SetRecoverFunc(func(ctx context.Context, err any) error {
228-
panicValue := ctx.Value(panicValueContextKey).(*any)
229-
*panicValue = err
236+
logger.Error("resolver panic", log.Any("error", err))
237+
230238
return fmt.Errorf("resolver panic: %v", err)
231239
})
232240

@@ -253,20 +261,7 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg
253261
},
254262
)
255263

256-
return WithSession(usrmgrSvc, authCfg, func(w http.ResponseWriter, r *http.Request) {
257-
ctx := r.Context()
258-
259-
// Hack to capture the panic value, because gqlgen execute resolver in a different goroutine.
260-
// And I want use the go.gearno.de/kit/httpserver built in panic recovery.
261-
var panicValue any
262-
ctx = context.WithValue(ctx, panicValueContextKey, &panicValue)
263-
264-
srv.ServeHTTP(w, r.WithContext(ctx))
265-
266-
if panicValue != nil {
267-
panic(panicValue)
268-
}
269-
})
264+
return WithSession(usrmgrSvc, authCfg, srv.ServeHTTP)
270265
}
271266

272267
func WithSession(usrmgrSvc *usrmgr.Service, authCfg AuthConfig, next http.HandlerFunc) http.HandlerFunc {

pkg/server/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/getprobo/probo/pkg/server/web"
2828
"github.com/getprobo/probo/pkg/usrmgr"
2929
"github.com/go-chi/chi/v5"
30+
"go.gearno.de/kit/log"
3031
)
3132

3233
// Config holds the configuration for the server
@@ -37,6 +38,7 @@ type Config struct {
3738
Auth console_v1.AuthConfig
3839
ConnectorRegistry *connector.ConnectorRegistry
3940
SafeRedirect *saferedirect.SafeRedirect
41+
Logger *log.Logger
4042
}
4143

4244
// Server represents the main server that handles both API and frontend requests
@@ -56,6 +58,7 @@ func NewServer(cfg Config) (*Server, error) {
5658
Auth: cfg.Auth,
5759
ConnectorRegistry: cfg.ConnectorRegistry,
5860
SafeRedirect: cfg.SafeRedirect,
61+
Logger: cfg.Logger.Named("api"),
5962
}
6063
apiServer, err := api.NewServer(apiCfg)
6164
if err != nil {

0 commit comments

Comments
 (0)