Skip to content

Commit

Permalink
Merge pull request #212 from bugsnag/daria-PLAT-11611
Browse files Browse the repository at this point in the history
Fix middleware panic on nil *http.Request
  • Loading branch information
DariaKunoichi committed Feb 20, 2024
2 parents e46c929 + c4272c2 commit 3804faf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 2.2.1 (2022-02-21)

### Bug fixes

* Fix middleware panic on nil *http.Request
[#212](https://github.com/bugsnag/bugsnag-go/pull/212)

## 2.2.0 (2022-10-12)

### Enhancements
Expand Down
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func catchMiddlewarePanic(event *Event, config *Configuration, next func() error
// use this as a template for writing your own Middleware.
func httpRequestMiddleware(event *Event, config *Configuration) error {
for _, datum := range event.RawData {
if request, ok := datum.(*http.Request); ok {
if request, ok := datum.(*http.Request); ok && request != nil {
event.MetaData.Update(MetaData{
"request": {
"params": request.URL.Query(),
Expand Down
2 changes: 1 addition & 1 deletion v2/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (stack *middlewareStack) runBeforeFilter(f beforeFunc, event *Event, config
// use this as a template for writing your own Middleware.
func httpRequestMiddleware(event *Event, config *Configuration) error {
for _, datum := range event.RawData {
if request, ok := datum.(*http.Request); ok {
if request, ok := datum.(*http.Request); ok && request != nil {
event.MetaData.Update(MetaData{
"request": {
"params": request.URL.Query(),
Expand Down
16 changes: 15 additions & 1 deletion v2/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package bugsnag
import (
"bytes"
"fmt"
"github.com/bugsnag/bugsnag-go/v2/errors"
"log"
"net/http"
"reflect"
"testing"

"github.com/bugsnag/bugsnag-go/v2/errors"
)

func TestMiddlewareOrder(t *testing.T) {
Expand Down Expand Up @@ -95,3 +97,15 @@ func TestBeforeNotifyPanic(t *testing.T) {
t.Errorf("Notify was not called when BeforeNotify panicked")
}
}

func TestHttpRequestMiddleware(t *testing.T) {
var req *http.Request
rawData := []interface{}{req}

event := &Event{RawData: rawData}
config := &Configuration{}
err := httpRequestMiddleware(event, config)
if err != nil {
t.Errorf("Should not happen")
}
}

0 comments on commit 3804faf

Please sign in to comment.