line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::PostErrorToSlack; |
2
|
2
|
|
|
2
|
|
2114
|
use 5.008001; |
|
2
|
|
|
|
|
6
|
|
3
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
45
|
|
4
|
2
|
|
|
2
|
|
74
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
113
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.01"; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
792
|
use Plack::Util::Accessor qw(webhook_url channel username icon_url icon_emoji); |
|
2
|
|
|
|
|
252
|
|
|
2
|
|
|
|
|
18
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
859
|
use parent qw(Plack::Middleware); |
|
2
|
|
|
|
|
332
|
|
|
2
|
|
|
|
|
12
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
15475
|
use Plack::Response; |
|
2
|
|
|
|
|
20634
|
|
|
2
|
|
|
|
|
62
|
|
13
|
2
|
|
|
2
|
|
1197
|
use Try::Tiny; |
|
2
|
|
|
|
|
1640
|
|
|
2
|
|
|
|
|
124
|
|
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
2
|
|
271490
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
83808
|
|
|
2
|
|
|
|
|
78
|
|
16
|
2
|
|
|
2
|
|
1087
|
use JSON::XS qw(encode_json); |
|
2
|
|
|
|
|
6280
|
|
|
2
|
|
|
|
|
895
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub call { |
19
|
3
|
|
|
3
|
1
|
25643
|
my $self = shift; |
20
|
3
|
|
|
|
|
6
|
my $env = shift; |
21
|
|
|
|
|
|
|
|
22
|
3
|
|
|
|
|
5
|
my $error; |
23
|
|
|
|
|
|
|
my $res = try { |
24
|
3
|
|
|
3
|
|
91
|
$self->app->($env); |
25
|
|
|
|
|
|
|
} catch { |
26
|
2
|
|
|
2
|
|
52
|
$error = $_; |
27
|
3
|
|
|
|
|
31
|
}; |
28
|
3
|
100
|
|
|
|
102
|
if ($error) { |
29
|
2
|
|
|
|
|
7
|
$self->post_error($env, $error); |
30
|
2
|
|
|
|
|
289
|
die $error; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
7
|
return $res; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub post_error { |
37
|
2
|
|
|
2
|
0
|
5
|
my ($self, $env, $error) = @_; |
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
7
|
my $message = $self->error_message($env, $error); |
40
|
|
|
|
|
|
|
|
41
|
2
|
|
|
|
|
125
|
my $ua = LWP::UserAgent->new; |
42
|
|
|
|
|
|
|
|
43
|
2
|
50
|
|
|
|
7666
|
unless ($self->webhook_url) { |
44
|
0
|
|
|
|
|
0
|
warn 'Please set webhook_url'; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
|
|
56
|
my $payload = { |
48
|
|
|
|
|
|
|
text => $message, |
49
|
|
|
|
|
|
|
}; |
50
|
2
|
|
|
|
|
12
|
for my $key (qw{channel username icon_url icon_emoji}) { |
51
|
8
|
|
|
|
|
51
|
my $value = $self->$key; |
52
|
8
|
100
|
|
|
|
88
|
next unless $value; |
53
|
4
|
|
|
|
|
15
|
$payload->{$key} = $value; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
2
|
|
|
|
|
13
|
$ua->post($self->webhook_url, { |
57
|
|
|
|
|
|
|
payload => encode_json($payload), |
58
|
|
|
|
|
|
|
}); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub error_message { |
62
|
2
|
|
|
2
|
0
|
4
|
my ($self, $env, $error) = @_; |
63
|
|
|
|
|
|
|
|
64
|
2
|
|
|
|
|
6
|
my $user = $ENV{'USER'}; |
65
|
|
|
|
|
|
|
|
66
|
2
|
|
|
|
|
94579
|
my $branch = `git rev-parse --abbrev-ref HEAD`; |
67
|
2
|
|
|
|
|
29
|
chomp $branch; |
68
|
|
|
|
|
|
|
|
69
|
2
|
|
|
|
|
17
|
$error = $error . ''; # copy; |
70
|
2
|
|
|
|
|
13
|
chomp $error; |
71
|
|
|
|
|
|
|
|
72
|
2
|
50
|
|
|
|
39
|
if ($branch) { |
73
|
0
|
|
|
|
|
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
|
|
|
|
|
240
|
sprintf "%s encountered an error while `%s %s`\n```\n%s\n```", $user, $env->{REQUEST_METHOD}, $env->{PATH_INFO}, $error; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
__END__ |