diff --git a/CHANGELOG.md b/CHANGELOG.md index fe5ba62..acbb2a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,13 @@ [#217](https://github.com/bugsnag/bugsnag-go/pull/217) [Chris Duncan](https://github.com/veqryn) +## 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 diff --git a/middleware.go b/middleware.go index 3eac864..b5018b8 100644 --- a/middleware.go +++ b/middleware.go @@ -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(), diff --git a/v2/bugsnag.go b/v2/bugsnag.go index d35d82a..deef53c 100644 --- a/v2/bugsnag.go +++ b/v2/bugsnag.go @@ -21,7 +21,7 @@ import ( ) // Version defines the version of this Bugsnag notifier -const Version = "2.2.0" +const Version = "2.2.1" var panicHandlerOnce sync.Once var sessionTrackerOnce sync.Once @@ -91,11 +91,13 @@ func Notify(err error, rawData ...interface{}) error { // The rawData is used to send extra information along with any // panics that are handled this way. // Usage: -// go func() { -// ctx := bugsnag.StartSession(context.Background()) -// defer bugsnag.AutoNotify(ctx) -// // (possibly crashy code) -// }() +// +// go func() { +// ctx := bugsnag.StartSession(context.Background()) +// defer bugsnag.AutoNotify(ctx) +// // (possibly crashy code) +// }() +// // See also: bugsnag.Recover() func AutoNotify(rawData ...interface{}) { if err := recover(); err != nil { @@ -122,18 +124,22 @@ func AutoNotify(rawData ...interface{}) { // The rawData is used to send extra information along with // any panics that are handled this way // Usage: -// go func() { -// ctx := bugsnag.StartSession(context.Background()) -// defer bugsnag.Recover(ctx) -// // (possibly crashy code) -// }() +// +// go func() { +// ctx := bugsnag.StartSession(context.Background()) +// defer bugsnag.Recover(ctx) +// // (possibly crashy code) +// }() +// // If you wish that any panics caught by the call to Recover shall affect your // stability score (it does not by default): -// go func() { -// ctx := bugsnag.StartSession(context.Background()) -// defer bugsnag.Recover(ctx, bugsnag.HandledState{Unhandled: true}) -// // (possibly crashy code) -// }() +// +// go func() { +// ctx := bugsnag.StartSession(context.Background()) +// defer bugsnag.Recover(ctx, bugsnag.HandledState{Unhandled: true}) +// // (possibly crashy code) +// }() +// // See also: bugsnag.AutoNotify() func Recover(rawData ...interface{}) { if err := recover(); err != nil { diff --git a/v2/middleware.go b/v2/middleware.go index 2ed721a..2dc0ac9 100644 --- a/v2/middleware.go +++ b/v2/middleware.go @@ -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(), diff --git a/v2/middleware_test.go b/v2/middleware_test.go index e7a3ad3..ccd943d 100644 --- a/v2/middleware_test.go +++ b/v2/middleware_test.go @@ -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) { @@ -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") + } +}