Skip to content

Commit

Permalink
fix: Check for initialized Client in AddBreadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyo authored and kamilogorek committed Jul 1, 2019
1 parent 69c26e4 commit c8176bd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
17 changes: 12 additions & 5 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,22 @@ func (hub *Hub) CaptureException(exception error) *EventID {
// The total number of breadcrumbs that can be recorded are limited by the
// configuration on the client.
func (hub *Hub) AddBreadcrumb(breadcrumb *Breadcrumb, hint *BreadcrumbHint) {
options := hub.Client().Options()
maxBreadcrumbs := defaultMaxBreadcrumbs
client := hub.Client()

// If there's no client, just store it on the scope straight away
if client == nil {
hub.Scope().AddBreadcrumb(breadcrumb, maxBreadcrumbs)
return
}

options := client.Options()
max := defaultMaxBreadcrumbs

if options.MaxBreadcrumbs != 0 {
maxBreadcrumbs = options.MaxBreadcrumbs
max = options.MaxBreadcrumbs
}

if maxBreadcrumbs < 0 {
if max < 0 {
return
}

Expand All @@ -203,7 +211,6 @@ func (hub *Hub) AddBreadcrumb(breadcrumb *Breadcrumb, hint *BreadcrumbHint) {
}
}

max := maxBreadcrumbs
if max > maxBreadcrumbs {
max = maxBreadcrumbs
}
Expand Down
25 changes: 25 additions & 0 deletions hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,31 @@ func TestAddBreadcrumbSkipAllBreadcrumbsIfMaxBreadcrumbsIsLessThanZero(t *testin
assertEqual(t, len(scope.breadcrumbs), 0)
}

func TestAddBreadcrumbShouldNeverExceedMaxBreadcrumbsConst(t *testing.T) {
hub, client, scope := setupHubTest()
client.options.MaxBreadcrumbs = 1000

breadcrumb := &Breadcrumb{Message: "Breadcrumb"}

for i := 0; i < 111; i++ {
hub.AddBreadcrumb(breadcrumb, nil)
}

assertEqual(t, len(scope.breadcrumbs), 100)
}

func TestAddBreadcrumbShouldWorkWithoutClient(t *testing.T) {
scope := NewScope()
hub := NewHub(nil, scope)

breadcrumb := &Breadcrumb{Message: "Breadcrumb"}
for i := 0; i < 111; i++ {
hub.AddBreadcrumb(breadcrumb, nil)
}

assertEqual(t, len(scope.breadcrumbs), 100)
}

func TestAddBreadcrumbCallsBeforeBreadcrumbCallback(t *testing.T) {
hub, client, scope := setupHubTest()
client.options.BeforeBreadcrumb = func(breadcrumb *Breadcrumb, hint *BreadcrumbHint) *Breadcrumb {
Expand Down

0 comments on commit c8176bd

Please sign in to comment.