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

fix: Check for initialized Client in AddBreadcrumbs #20

Merged
merged 1 commit into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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