Skip to content

Commit

Permalink
prevent nil panics
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJenicek committed Sep 30, 2024
1 parent 2029ca9 commit a95b8c4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion helpers.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down

0 comments on commit a95b8c4

Please sign in to comment.