Skip to content

Commit

Permalink
Merge pull request #17 from TakeoutCentral/fix/capture_context
Browse files Browse the repository at this point in the history
Fix `capture_context` for `capture_exception`. Add to `capture_event`
  • Loading branch information
pmb0 authored Oct 6, 2023
2 parents 18af840 + f17a5a4 commit be826dc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/Sentry/Hub/Scope.pm
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ sub clone ($self) {
}

sub update ($self, $fields) {
$self->$_($fields->{$_}) for keys $fields->%*;
for (keys $fields->%*) {
my $methodName = "set_$_";
$self->$methodName($fields->{$_});
}
return $self;
}

sub get_global_event_processors () {
Expand Down
8 changes: 5 additions & 3 deletions lib/Sentry/SDK.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ sub capture_message ($self, $message, $capture_context = undef) {
{ capture_context => ref($capture_context) ? $capture_context : undef, });
}

sub capture_event ($package, $event) {
_call_on_hub('capture_event', $event);
sub capture_event ($package, $event, $capture_context = undef) {
_call_on_hub('capture_event', $event,
{ capture_context => ref($capture_context) ? $capture_context : undef, });
}

sub capture_exception ($package, $exception, $capture_context = undef) {
_call_on_hub('capture_exception', $exception, $capture_context);
_call_on_hub('capture_exception', $exception,
{ capture_context => ref($capture_context) ? $capture_context : undef, });
}

sub configure_scope ($package, $cb) {
Expand Down
42 changes: 42 additions & 0 deletions t/client.t
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,48 @@ describe 'Sentry::Client' => sub {
};
};

describe 'capture_event()' => sub {
it 'event only' => sub {
my $event = { event => 'happened' };
$client->capture_event($event);

is $transport->events_sent->@*, 1;

my %event = $transport->events_sent->[0]->%*;
ok defined $event{event_id};
ok looks_like_number($event{timestamp});
is $event{event} => 'happened';
};

it 'w/ scope' => sub {
my %tags = (foo => 'bar', bar => 'baz');
my $scope = Sentry::Hub::Scope->new(tags => {%tags});
my $event = { event => 'happened' };
$client->capture_event($event, undef, $scope);

is $transport->events_sent->@*, 1, 'Event was sent';

my %event = $transport->events_sent->[0]->%*;
is_deeply $event{tags}, \%tags;
};

it 'updates scope' => sub {
my %tags = (foo => 'bar', bar => 'baz');
my $scope = Sentry::Hub::Scope->new(tags => {%tags});
my $event = { event => 'happened' };
$client->capture_event(
$event,
{ capture_context => { tags => { foo => 'baz' } } },
$scope
);

is $transport->events_sent->@*, 1, 'Event was sent';

my %event = $transport->events_sent->[0]->%*;
is_deeply $event{tags}, { %tags, foo => 'baz' };
};
};

describe 'before_send' => sub {
it 'before_send alters the event object' => sub {
$client->_options->{before_send} = sub ($event, $hint) {
Expand Down
8 changes: 6 additions & 2 deletions t/sdk.t
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,26 @@ describe 'Sentry::SDK' => sub {

it 'capture_event()' => sub {
my $event = { foo => 'bar' };
Sentry::SDK->capture_event($event);
my $context = { level => Sentry::Severity->Error };
Sentry::SDK->capture_event($event, $context);

my $captured = $client->_captured_message;
isa_ok $captured->{scope}, 'Sentry::Hub::Scope';
is_deeply $captured->{event}, $event;
is_deeply $captured->{hint}{capture_context}, $context;
is_UUID_string $captured->{hint}{event_id};
};

it 'capture_exception()' => sub {
my $exception = Mojo::Exception->new('ohoh');
Sentry::SDK->capture_exception($exception);
my $context = { level => Sentry::Severity->Warning };
Sentry::SDK->capture_exception($exception, $context);

my $captured = $client->_captured_message;
isa_ok $captured->{scope}, 'Sentry::Hub::Scope';
is_deeply $captured->{exception}, $exception;
is_deeply $captured->{hint}{original_exception} => $exception;
is_deeply $captured->{hint}{capture_context}, $context;
is_UUID_string $captured->{hint}{event_id};
};
};
Expand Down

0 comments on commit be826dc

Please sign in to comment.