replace Catalyst::Plugin::ErrorCatcher with a call to the SGN::View::Email::ErrorEmai...
[sgn.git] / lib / SGN / View / Email / ErrorEmail.pm
blobb0961439fc05364403c44bce4fc0af8432ad9673
1 package SGN::View::Email::ErrorEmail;
2 use Moose;
3 use Moose::Util::TypeConstraints;
5 use Data::Dump ();
6 use Data::Visitor::Callback;
8 =head1 NAME
10 SGN::View::Email::ErrorEmail - Email View for SGN
12 =head1 DESCRIPTION
14 View for sending error emails from SGN.
16 =cut
18 BEGIN { extends 'SGN::View::Email' }
20 before 'process' => sub {
21 my ($self, $c) = @_;
23 # convert the notify_errors stash key into an email stash key for
24 # the generic email view
25 $c->stash->{email} = {
26 to => $self->default->{to},
27 from => $self->default->{from},
29 body => $self->_email_body( $c ),
32 $c->log->debug('sending error email to '.$c->stash->{email}->{to}) if $c->debug;
36 sub _email_body {
37 my ( $self, $c ) = @_;
38 my $error_num = 1;
39 return join '',
41 # the errors
42 "==== Error(s) ====\n\n",
43 ( map { $error_num++.". $_\n" } @{$c->stash->{email_errors}} ),
45 # all the necessary debug information
46 ( map { ("\n==== $_->[0] ====\n", $_->[1], "\n") } $self->dump_these_strings( $c ) );
50 =head1 ATTRIBUTES
52 =head2 debug_filter_visitor
54 The L<Data::Visitor::Callback> object being used for filtering. Can
55 be replaced with your own L<Data::Visitor> subclass if desired.
57 =cut
59 has 'debug_filter_visitor' => (
60 is => 'rw',
61 isa => 'Data::Visitor',
62 lazy_build => 1,
64 sub _build_debug_filter_visitor {
65 my ($self) = @_;
67 return Data::Visitor::Callback->new(
69 # descend into objects also
70 object => 'visit_ref',
72 # render skip_class option as visitor args
73 ( map {
74 my $class = $_;
75 $class => sub { shift; '('.ref(shift)." object skipped, isa $class)" }
76 } @{ $self->dump_skip_class }
79 #render any other visitor args
80 %{ $self->dump_visitor_args },
85 =head2 dump_skip_class
87 One or more class names to filter out of objects to be dumped. If an
88 object is-a one of these classes, the dump filtering will replace
89 the object with a string "skipped" message.
91 Can be either an arrayref or a whitespace-separated list of class names.
93 Default: "Catalyst", which will filter out Catalyst context objects.
95 =head2 dump_visitor_args
97 Hashref of additional constructor args passed to the
98 L<Data::Visitor::Callback> object used to filter the objects for
99 dumping. Can be used to introduce nearly any kind of additional
100 filtering desired.
102 Example:
104 # replace all scalar values in dumped objects with "chicken"
105 DebugFilter => {
106 visitor_args => {
107 value => sub { 'Chicken' },
111 =cut
113 { my $sc = subtype as 'ArrayRef';
114 coerce $sc, from 'Str', via { [ split ] };
115 has 'dump_skip_class' => (
116 is => 'ro',
117 isa => $sc,
118 coerce => 1,
119 default => sub { ['Catalyst'] },
123 has 'dump_visitor_args' => (
124 is => 'ro',
125 isa => 'HashRef',
126 default => sub { {} },
129 =head1 METHODS
131 =head2 dump_these_strings( $c )
133 Get a list like
134 C<['Request', 'string dump'], ['Stash', 'string dump'], ...>
135 for use in debugging output.
137 =cut
139 sub dump_these_strings {
140 my ($self,$c) = @_;
141 return
142 map [ $_->[0], Data::Dump::dump( $self->filter_object_for_dump( $_->[1] ) ) ],
143 $c->dump_these;
146 =head2 filter_object_for_dump( $object )
148 Return a filtered copy of the given object.
150 =cut
152 sub filter_object_for_dump {
153 my ( $self, $object ) = @_;
154 $self->debug_filter_visitor->visit( $object );
157 =head1 AUTHOR
159 Robert Buels
161 =cut