From 2d80ed15e12dccf67236eed1f9cfec919ad5e21c Mon Sep 17 00:00:00 2001 From: Lukas Jenicek Date: Mon, 30 Sep 2024 15:28:04 +0200 Subject: [PATCH] prevent nil panics --- helpers.go.tmpl | 5 ++++- server.go.tmpl | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/helpers.go.tmpl b/helpers.go.tmpl index 3a7f2ad..99384d3 100644 --- a/helpers.go.tmpl +++ b/helpers.go.tmpl @@ -54,7 +54,10 @@ func RequestFromContext(ctx context.Context) *http.Request { func MethodFromContext(ctx context.Context) MethodCtx { name, _ := ctx.Value(MethodNameCtxKey).(string) service, _ := ctx.Value(ServiceNameCtxKey).(string) - annotations, _ := ctx.Value(methodAnnotationsCtxKey).(map[string]string) + annotations, ok := ctx.Value(methodAnnotationsCtxKey).(map[string]string) + if !ok { + annotations = map[string]string{} + } return MethodCtx{ Name: name, diff --git a/server.go.tmpl b/server.go.tmpl index 5c0a77a..6589f00 100644 --- a/server.go.tmpl +++ b/server.go.tmpl @@ -71,7 +71,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch contentType { case "application/json": - if s.OnRequest != nil { + if s.OnRequest != nil { if err := s.OnRequest(w, r); err != nil { rpcErr, ok := err.(WebRPCError) if !ok { @@ -80,7 +80,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.sendErrorJSON(w, r, rpcErr) return } - } + } handler(ctx, w, r) default: