| 1 | package Catalyst::Plugin::ErrorCatcher::Email; | ||||||||||||||||||||||||||||
| 2 | 2 | $Catalyst::Plugin::ErrorCatcher::Email::VERSION = '0.0.8.18'; | |||||||||||||||||||||||||||
| 3 | { | ||||||||||||||||||||||||||||
| 4 | 2 | $Catalyst::Plugin::ErrorCatcher::Email::DIST = 'Catalyst-Plugin-ErrorCatcher'; | |||||||||||||||||||||||||||
| 5 | } | ||||||||||||||||||||||||||||
| 6 | # ABSTRACT: an email emitter for Catalyst::Plugin::ErrorCatcher | ||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use MIME::Lite; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Path::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Sys::Hostname; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub emit { | ||||||||||||||||||||||||||
|
Subroutine
emit: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 0 | my ($class, $c, $output) = @_; | |||||||||||||||||||||||||||
| 16 | 0 | my ($config, $msg); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # check and tidy the config | ||||||||||||||||||||||||||||
| 19 | 0 | $config = _check_config($c, $config); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # build the message | ||||||||||||||||||||||||||||
| 22 | my %msg_config = ( | ||||||||||||||||||||||||||||
| 23 | From => $config->{from}, | ||||||||||||||||||||||||||||
| 24 | To => $config->{to}, | ||||||||||||||||||||||||||||
| 25 | Subject => $config->{subject}, | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | Type => 'TEXT', | |||||||||||||||||||||||||||
| 28 | Data => $output, | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | # add the optional Cc value | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if (exists $config->{cc}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | $msg_config{Cc} = $config->{cc}; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 0 | $msg = MIME::Lite->new( %msg_config ); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # send the message | ||||||||||||||||||||||||||||
| 37 | 0 | _send_email($msg, $config); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | return; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub _check_config { | ||||||||||||||||||||||||||
|
Subroutine
_check_config: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 43 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | my $config = $c->_errorcatcher_c_cfg->{"Plugin::ErrorCatcher::Email"}; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # no config, no email | ||||||||||||||||||||||||||||
| 48 | # we die so we count as a failure | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if (not defined $config) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | die "Catalyst::Plugin::ErrorCatcher::Email has no configuration\n"; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # no To:, no email | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if (not defined $config->{to}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | die "Catalyst::Plugin::ErrorCatcher::Email has no To: address\n"; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # set a default From address | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if (not defined $config->{from}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | $config->{from} = $config->{to}; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # allow people to put Magic Tags into the subject line | ||||||||||||||||||||||||||||
| 64 | # (nifty idea suggested by pecastro) | ||||||||||||||||||||||||||||
| 65 | # only use them if we have a user subject *AND* they've asked us to work | ||||||||||||||||||||||||||||
| 66 | # the magic on it | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $config->{"subject"} and $config->{"use_tags"}
MC/DC0%0/2
defined $config->{"subject"} and $config->{"use_tags"}
defined $config->{"subject"}
$config->{"use_tags"}
Truth table0%0/3
A and B
A= defined $config->{"subject"}B= $config->{"use_tags"}
| |||||||||||||||||||||||||||||
| 68 | defined $config->{subject} | ||||||||||||||||||||||||||||
| 69 | && $config->{use_tags} | ||||||||||||||||||||||||||||
| 70 | ) { | ||||||||||||||||||||||||||||
| 71 | $config->{subject} = | ||||||||||||||||||||||||||||
| 72 | 0 | _parse_tags($c, $config->{subject}); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # set a default Subject-Line | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if (not defined $config->{subject}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | $config->{subject} = | ||||||||||||||||||||||||||||
| 78 | q{Error Report for } | ||||||||||||||||||||||||||||
| 79 | . $c->config->{name} | ||||||||||||||||||||||||||||
| 80 | 0 | ; | |||||||||||||||||||||||||||
| 81 | 0 | my $host = Sys::Hostname::hostname(); | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | if (defined $host) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | $config->{subject} .= | ||||||||||||||||||||||||||||
| 84 | 0 | q{ on } | |||||||||||||||||||||||||||
| 85 | . $host | ||||||||||||||||||||||||||||
| 86 | ; | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | return $config; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # supported tags | ||||||||||||||||||||||||||||
| 94 | # %h server hostname | ||||||||||||||||||||||||||||
| 95 | # %f filename where error occurred | ||||||||||||||||||||||||||||
| 96 | # %F filename where error occurred, leading directories trimmed | ||||||||||||||||||||||||||||
| 97 | # %l line number where error occurred | ||||||||||||||||||||||||||||
| 98 | # %p package where error occurred | ||||||||||||||||||||||||||||
| 99 | # %V application version (if set) | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub _parse_tags { | ||||||||||||||||||||||||||
|
Subroutine
_parse_tags: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 101 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 102 | 0 | my $subject = shift; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my $first_frame = $c->_errorcatcher_first_frame || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$c->_errorcatcher_first_frame || {}
MC/DC0%0/1
$c->_errorcatcher_first_frame || {}
$c->_errorcatcher_first_frame
Truth table0%0/2
A
A= $c->_errorcatcher_first_frame
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | my %tag_of = ( | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | '%h' => sub{Sys::Hostname::hostname()||'UnknownHost'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | '%f' => sub{$first_frame->{file}||'UnknownFile'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | '%F' => sub{ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $val=$first_frame->{file}||'UnknownFile'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$first_frame->{'file'} || 'UnknownFile'
MC/DC0%0/1
$first_frame->{'file'} || 'UnknownFile'
$first_frame->{'file'}
Truth table0%0/2
A
A= $first_frame->{'file'}
| |||||||||||||||||||||||||||||
| 111 | # ideally replace with cross-platform directory separator | ||||||||||||||||||||||||||||
| 112 | 0 | return _munge_path($val); | |||||||||||||||||||||||||||
| 113 | }, | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | '%l' => sub{$first_frame->{line}||'UnknownLine'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | '%p' => sub{$first_frame->{pkg}||'UnknownPackage'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | '%V' => sub{$c->config->{version}||'UnknownVersion'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | '%n' => sub{$c->config->{name}||'UnknownAppName'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | ); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | foreach my $tag (keys %tag_of) { | |||||||||||||||||||||||||||
| 121 | 0 | $subject =~ s{$tag}{&{$tag_of{$tag}}}eg; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | return $subject; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub _send_email { | ||||||||||||||||||||||||||
|
Subroutine
_send_email: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 128 | 0 | my $msg = shift; | |||||||||||||||||||||||||||
| 129 | 0 | my $config = shift; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # if there are specific send options, use them | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if (exists $config->{send}{type} and exists $config->{send}{args}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $config->{"send"}{"type"} and exists $config->{"send"}{"args"}
MC/DC0%0/2
exists $config->{"send"}{"type"} and exists $config->{"send"}{"args"}
exists $config->{"send"}{"type"}
exists $config->{"send"}{"args"}
Truth table0%0/3
A and B
A= exists $config->{"send"}{"type"}B= exists $config->{"send"}{"args"}
| |||||||||||||||||||||||||||||
| 133 | $msg->send( | ||||||||||||||||||||||||||||
| 134 | $config->{send}{type}, | ||||||||||||||||||||||||||||
| 135 | 0 | @{ $config->{send}{args} } | |||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||
| 137 | 0 | return; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # use default send method | ||||||||||||||||||||||||||||
| 141 | 0 | $msg->send; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | return; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 2 | ▶ | sub _munge_path { | ||||||||||||||||||||||||||
|
Subroutine
_munge_path: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 147 | 2 | my $path_string = shift; | |||||||||||||||||||||||||||
| 148 | 2 | my $path_spec = Path::Class::dir($path_string); | |||||||||||||||||||||||||||
| 149 | 2 | my $path_re = qr{^(?:lib|script)$}; | |||||||||||||||||||||||||||
| 150 | # | ||||||||||||||||||||||||||||
| 151 | # return $path_string | ||||||||||||||||||||||||||||
| 152 | # if not grep { /${path_re}/ } $path_spec->dir_list; | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2 | my @dirs = $path_spec->dir_list; | |||||||||||||||||||||||||||
| 155 | 2 | my @new_dirs = (); | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # work backwards through the path (it should be shorter) | ||||||||||||||||||||||||||||
| 158 | # pop of everything until we match or exhaust the list | ||||||||||||||||||||||||||||
| 159 | # (which we shouldn't because we already checked for a match) | ||||||||||||||||||||||||||||
| 160 | 2 | ▶ | while ( @dirs && $dirs[-1] !~ m/${path_re}/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@dirs and not $dirs[-1] =~ /$path_re/
MC/DC100%2/2
@dirs and not $dirs[-1] =~ /$path_re/
@dirs
not $dirs[-1] =~ /$path_re/
Truth table100%3/3
A and not(B)
A= @dirsB= not $dirs[-1] =~ /$path_re/
| |||||||||||||||||||||||||||||
| 161 | 12 | unshift @new_dirs, pop @dirs; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # build a path for the list we built up and return it | ||||||||||||||||||||||||||||
| 165 | 2 | return Path::Class::dir(@new_dirs)->stringify; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 2 | 1; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =pod | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 NAME | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Catalyst::Plugin::ErrorCatcher::Email - an email emitter for Catalyst::Plugin::ErrorCatcher | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 VERSION | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | version 0.0.8.18 | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | In your application: | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | use Catalyst qw/-Debug StackTrace ErrorCatcher/; | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | In your application configuration: | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | <Plugin::ErrorCatcher> | ||||||||||||||||||||||||||||
| 191 | # ... | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | emit_module Catalyst::Plugin::ErrorCatcher::Email | ||||||||||||||||||||||||||||
| 194 | </Plugin::ErrorCatcher> | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | <Plugin::ErrorCatcher::Email> | ||||||||||||||||||||||||||||
| 197 | to address@example.com | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # defaults to the To: address | ||||||||||||||||||||||||||||
| 200 | from another@example.com | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # defaults to "Error Report For <AppName>" | ||||||||||||||||||||||||||||
| 203 | subject Alternative Subject Line | ||||||||||||||||||||||||||||
| 204 | </Plugin::ErrorCatcher::Email> | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | SUBJECT LINE TAGS | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | There are some tags which can be used in the subject line which will be | ||||||||||||||||||||||||||||
| 209 | replaced with appropriate values. You need to enable tag parsing in your | ||||||||||||||||||||||||||||
| 210 | configuration: | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | <Plugin::ErrorCatcher::Email> | ||||||||||||||||||||||||||||
| 213 | # ... | ||||||||||||||||||||||||||||
| 214 | use_tags 1 | ||||||||||||||||||||||||||||
| 215 | </Plugin::ErrorCatcher::Email> | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Available tags are: | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | %f filename where error occurred | ||||||||||||||||||||||||||||
| 220 | %F filename where error occurred, leading directories trimmed | ||||||||||||||||||||||||||||
| 221 | %h server hostname | ||||||||||||||||||||||||||||
| 222 | %l line number where error occurred | ||||||||||||||||||||||||||||
| 223 | %n application name | ||||||||||||||||||||||||||||
| 224 | %p package where error occurred | ||||||||||||||||||||||||||||
| 225 | %V application version | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Allowing you to set your subject like this: | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | <Plugin::ErrorCatcher::Email> | ||||||||||||||||||||||||||||
| 230 | # ... | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | subject Report from: %h; %F, line %l | ||||||||||||||||||||||||||||
| 233 | </Plugin::ErrorCatcher::Email> | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =head2 emit($class, $c, $output) | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | Emit the error report by email. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | Chisel <chisel@chizography.net> | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | This software is copyright (c) 2015 by Chisel Wright. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 248 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =cut | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | __END__ | ||||||||||||||||||||||||||||