blib/lib/Plack/Middleware/PostErrorToSlack.pm

filter: Statementstmt 96.5% 56 / 58 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 33.3% 1 / 3 totaltotal 92.7% 77 / 83 CC 6 SCAR 6.2
CC
6
Cov
94%
post_error
4.0
error_message
2.0
call
2.0
CRAP 1.9
↑ Summary
1 package Plack::Middleware::PostErrorToSlack;
2 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 Plack::Util::Accessor qw(webhook_url channel username icon_url icon_emoji);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use JSON::XS qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶sub call {
Subroutine call: called CC=2 SCAR=6.9
Pod documented
19 3 my $self = shift;
20 3 my $env = shift;
21  
22 3 my $error;
23 3 ▶ my $res = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 3 $self->app->($env);
25 2 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 $error = $_;
27 3 };
28 3 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 2 1 3
29 2 $self->post_error($env, $error);
30 2 die $error;
31 }
32  
33 1 return $res;
34 }
35  
36 2 ▶sub post_error {
Subroutine post_error: called CC=4 SCAR=14.0
Pod undocumented
37 2 my ($self, $env, $error) = @_;
38  
39 2 my $message = $self->error_message($env, $error);
40  
41 2 my $ua = LWP::UserAgent->new;
42  
43 2 ▶ unless ($self->webhook_url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->webhook_url) 0 2 2
44 0 warn 'Please set webhook_url';
45 }
46  
47 2 my $payload = {
48 text => $message,
49 };
50 2 for my $key (qw{channel username icon_url icon_emoji}) {
51 8 my $value = $self->$key;
52 8 ▶ next unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 4 4 8
53 4 $payload->{$key} = $value;
54 }
55  
56 2 $ua->post($self->webhook_url, {
57 payload => encode_json($payload),
58 });
59 }
60  
61 2 ▶sub error_message {
Subroutine error_message: called CC=2 SCAR=7.1
Pod undocumented
62 2 my ($self, $env, $error) = @_;
63  
64 2 my $user = $ENV{'USER'};
65  
66 2 my $branch = `git rev-parse --abbrev-ref HEAD`;
67 2 chomp $branch;
68  
69 2 $error = $error . ''; # copy;
70 2 chomp $error;
71  
72 2 ▶ if ($branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch) { } 0 2 2
73 0 sprintf "%s encountered an error while `%s %s` on branch `%s`\n```\n%s\n```", $user, $env->{REQUEST_METHOD}, $env->{PATH_INFO}, $branch, $error;
74 } else {
75 2 sprintf "%s encountered an error while `%s %s`\n```\n%s\n```", $user, $env->{REQUEST_METHOD}, $env->{PATH_INFO}, $error;
76 }
77 }
78  
79 2 1;
80  
81 __END__
↑ Summary