blib/lib/Net/Airbrake.pm

filter: Statementstmt 96.8% 62 / 64 Branchbran 62.5% 5 / 8 Conditioncond 45.4% 5 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 85.3% 93 / 109 SCAR 25.1
CC
12
Cov
87%
send
7.3
BEGIN
3.5
_conceal_options
3.2
CRAP 12.3
↑ Summary blib/lib/Net/Airbrake/Request.pm »
1 package Net::Airbrake;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.01';
7  
8 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Scope::Guard qw(guard);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Net::Airbrake::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::Airbrake::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 use Class::Tiny {
15 api_key => undef,
16 project_id => undef,
17 environment => $ENV{AIRBRAKE_ENV} || $ENV{PLACK_ENV} || 'default',
18 1 ▶ _errors => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 ▶ _ua => sub { HTTP::Tiny->new(agent => "Net-Airbrake/$VERSION", timeout => 5) }
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 ▶};
Subroutine BEGIN: called CC=3 SCAR=12.5
Condition40%2/5
$ENV{'AIRBRAKE_ENV'} || $ENV{'PLACK_ENV'} || 'default'
l!l
02
$ENV{"AIRBRAKE_ENV"} or $ENV{"PLACK_ENV"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$ENV{'AIRBRAKE_ENV'} || $ENV{'PLACK_ENV'} || 'default'
$ENV{"AIRBRAKE_ENV"} $ENV{"PLACK_ENV"}
Truth table33%1/3
A or B
A= $ENV{"AIRBRAKE_ENV"}B= $ENV{"PLACK_ENV"}
ABresult
1X1
011
000
21  
22 sub add_error {
23 2 ▶ my $self = shift;
Subroutine add_error: called CC=1 SCAR=0.0
Pod documented
24 2 my ($error) = @_;
25  
26 2 push @{$self->_errors}, Net::Airbrake::Error->new($error);
27 }
28  
29 sub has_error {
30 2 ▶ scalar @{shift->_errors} ? 1 : 0;
Subroutine has_error: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @{(shift())->_errors;} ? : 2 0 2
31 }
32  
33 sub send {
34 2 ▶ my $self = shift;
Subroutine send: called CC=7 SCAR=19.9
Pod documented
35 2 my ($option) = @_;
36  
37 2 ▶ return unless $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_error 0 2 2
38  
39 2 $self->_conceal_options($option);
40  
41 my $context = {
42 os => $^O,
43 language => "Perl $^V",
44 environment => $self->environment,
45 2 ▶ %{$option->{context} || {}},
Branch: 1 branch
BranchTrueFalseTotal
$option->{"context"} or {} 2 0 2
46 };
47 my $req = Net::Airbrake::Request->new({
48 errors => $self->_errors,
49 context => $context,
50 environment => $option->{environment} || {},
51 session => $option->{session} || {},
52 params => $option->{params} || {},
53 2 ▶ });
Condition50%3/6
$option->{'environment'} || {}
l!l
02
$option->{'session'} || {}
l!l
02
$option->{'params'} || {}
l!l
02
MC/DC0%0/1
$option->{'environment'} || {}
$option->{'environment'}
MC/DC0%0/1
$option->{'session'} || {}
$option->{'session'}
MC/DC0%0/1
$option->{'params'} || {}
$option->{'params'}
Truth table50%1/2
A
A= $option->{'environment'}
Aresult
11
00
Truth table50%1/2
A
A= $option->{'session'}
Aresult
11
00
Truth table50%1/2
A
A= $option->{'params'}
Aresult
11
00
54  
55 2 ▶ my $guard = guard { $self->_errors([]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
56 2 my $res = $self->_ua->request(POST => $self->_url, {
57 content => $req->to_json,
58 headers => { 'Content-Type' => 'application/json' },
59 });
60 1 die "Request failed to Airbrake: @{[$res->{status}]} @{[$res->{reason}]} (@{[$res->{content}]})"
61 2 ▶ unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 1 1 2
62  
63 1 decode_json($res->{content});
64 }
65  
66 sub notify {
67 2 ▶ my $self = shift;
Subroutine notify: called CC=1 SCAR=0.0
Pod documented
68 2 my ($error, $option) = @_;
69  
70 2 $self->add_error($error);
71 2 $self->send($option);
72 }
73  
74 sub _url {
75 2 ▶ my $self = shift;
Subroutine _url: called CC=1 SCAR=0.0
76  
77 2 "https://airbrake.io/api/v3/projects/@{[$self->project_id]}/notices?key=@{[$self->api_key]}";
78 }
79  
80 sub _conceal_options {
81 2 ▶ my $self = shift;
Subroutine _conceal_options: called CC=3 SCAR=11.7
82 2 my ($option) = @_;
83  
84 2 for my $opt (qw(environment session params)) {
85 6 for my $key (grep { /(?:cookie|password)/i } keys %{$option->{$opt}}) {
86 0 $option->{$opt}{$key} =~ s/./*/g;
87 }
88 }
89 }
90  
91 2 1;
92 __END__
93  
94 =pod
95  
96 =head1 NAME
97  
98 Net::Airbrake - Airbrake Notifier API Client
99  
100 =head1 SYNOPSIS
101  
102 use Net::Airbrake;
103  
104 my $airbrake = Net::Airbrake->new(
105 api_key => 'xxxxxxx',
106 project_id => 9999999,
107 );
108  
109 eval { die 'Oops' };
110 $airbrake->notify($@);
111  
112 =head1 DESCRIPTION
113  
114 Net::Airbrake is a client of L<Airbrake|https://airbrake.io>.
115  
116 =head1 CLASS METHODS
117  
118 =head2 new(\%param)
119  
120 Create a new instance.
121  
122 =over 4
123  
124 =item api_key
125  
126 Required. API Key of your project.
127  
128 =item project_id
129  
130 Required. Project ID.
131  
132 =item environment
133  
134 Optional. The name of environment your application is running.
135 Default value is ueded $ENV{AIRBRAKE_ENV} or $ENV{PLACK_ENV} or 'default'.
136  
137 =back
138  
139 =head1 INSTANCE METHODS
140  
141 =head2 add_error($error)
142  
143 Add an error. $error accepts HashRef, Object, string generated by die() or just
144 string message. If you want to send raw error report, use HashRef included
145 following key/value pairs:
146  
147 =over 4
148  
149 =item type => String
150  
151 Required. Error type.
152  
153 =item message => String
154  
155 Required. Error message.
156  
157 =item backtrace => ArrayRef[HashRef]
158  
159 Optional. HashRef can contain followings key/value pairs:
160  
161 =over 8
162  
163 =item file => String
164  
165 File name an error occurred.
166  
167 =item line => Integer
168  
169 Line number an error occurred.
170  
171 =item function => String
172  
173 Subroutine name an error occurred.
174  
175 =back
176  
177 =back
178  
179 =head2 has_error()
180  
181 Return true value if the instance has been added error by add_error().
182  
183 =head2 send(\%option)
184  
185 Send errors added by add_error() to Airbrake. \%option parameter is optional and
186 all key/value pairs of its also optional. You can specify followings to report
187 error details.
188  
189 details
190  
191 =over 4
192  
193 =item context => HashRef
194  
195 Context values of its error.
196  
197 =item environment => HashRef
198  
199 Environment variables. Set HTTP Headers on web application as usual.
200  
201 =item session => HashRef
202  
203 Session values. Set your application session parameters.
204  
205 =item params => HashRef
206  
207 Request parameters. Set HTTP Request parameters on web application as usual.
208  
209 =back
210  
211 =head2 notify(\%error, \%option)
212  
213 Shortcut of add_error() and send() combination.
214  
215 =head1 SEE ALSO
216  
217 Notifier API V3 - L<https://help.airbrake.io/kb/api-2/notifier-api-v3>
218  
219 =head1 AUTHOR
220  
221 Six Apart, Ltd. E<lt>sixapart@cpan.orgE<gt>
222  
223 =head1 LICENSE
224  
225 This library is free software; you can redistribute it and/or modify
226 it under the same terms as Perl itself.
227  
228 =cut
↑ Summary blib/lib/Net/Airbrake/Request.pm »