Skip to content

Commit

Permalink
v2.2.1
Browse files Browse the repository at this point in the history
2.2.1 (2022-02-21)
Bug fixes: Fix middleware panic on nil *http.Request #212
  • Loading branch information
DariaKunoichi committed Feb 21, 2024
2 parents bdc1c88 + 8754ab3 commit 46a0b47
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 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 Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ GEM
multi_test (0.1.2)
os (1.0.1)
power_assert (2.0.0)
rack (2.0.9)
rack (2.0.9.3)
rake (12.3.3)
test-unit (3.2.9)
power_assert
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
38 changes: 22 additions & 16 deletions v2/bugsnag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
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 46a0b47

Please sign in to comment.