Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge 'master' into 'next' #219

Merged
merged 9 commits into from
Mar 5, 2024
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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")
}
}
Loading