Attached is the very beginning of what I hope will be CAP::ErrorPage.
Right now it's extremely basic (and COMPLETELY untested.) I'm just
looking some ideas for additional features and feedback on the options
available now.

In an effort to be template-agnostic, the module uses CGI.pm to
generate very basic HTML for the error page, or it calls a
user-supplied callback to render it.



Mike
package CGI::Application::Plugin::ErrorPage;

use strict;
use warnings;

use base 'Exporter';

our $VERSION = '0.01';

our @EXPORT = ( 'error_page', 
                'error_page_config' );

our $AUTOLOAD;

use Params::Validate ':all';
use Carp 'croak';
use Scalar::Util 'blessed';


my %CONF;
sub error_page_config { 
    my $class = shift;

    validate( @_, { title            => 0,
                    css_href         => 0,
                    render_callback  => CODEREF,
                    catch_rm         => 0 } );

    my %args = @_;

    $CONF{$class} = { @_ };

    # export custom AUTOLOAD runmode if they ask for it
    if( $CONF{$class}{catch_rm} ) { 
        no strict 'refs';
        *{ $class . '::AUTOLOAD' } = sub { 
            my $self = shift;

            # NOTE: we use CAP::EP's $AUTOLOAD since the code is compiled in this pkg
            my $fqsub = $AUTOLOAD;
            my ( $rm ) = ( split /::/, $fqsub )[-1];

            return $self->error_page( msg => "Invalid runmode $rm" );
        };
    }
}
            


sub error_page { 
    my $self = shift;

    validate( @_, { msg         => 1, 
                    title       => 0, 
                    css_href    => 0,
                  } 
            );

    my %args = ( title => 'Error', 
                 @_ 
               );

    # run the callback if we have one
    my $conf = $CONF{ blessed $self };
    if( my $cb = $conf->{render_callback} ) { 
        return $self->$cb( %args );
    }

    # otherwise generate the default page
    my $q = $self->query;

    my $ret = $q->start_html( 
      -title => $args{title},
      exists $args{css_href}
        ? ( -style => $args{css_href} )
        : ( ),
    );

    $ret .= $q->h1( { class => 'err_header' }, $args{title} );
    $ret .= $q->div( { class => 'err_msg' }, $args{msg} );
    $ret .= $q->end_html;

    return $ret;

}

1;

__END__


=head1 NAME

CGI::Application::Plugin::ErrorPage - An easy default error runmode for CGI::Application

=head1 VERSION

Version 0.01

=head1 SYNOPSIS

    package MyApp;
    
    use strict;
    use warnings;
    
    use base 'CGI::Application';
    
    use CGI::Application::Plugin::ErrorPage;
    
    __PACKAGE__->error_page_config( title     => 'Foobar App Error',
                                    css_href  => '/foobar.css' );
    
    ...
    
    sub show_stuff { 
        my $self = shift;
    
        if( $self->_explode ) {
            return $self->error_page( msg => "I exploded!" );
        }
    }


=head1 DESCRIPTION

This module is a L<CGI::Application> plugin which provides a simple error runmode for 
your application. When you want to report an error to the user, simply call C<error_page>
and pass the mandatory C<msg> parameter. The default error page is very simple but you can
customize it with CSS or by providing your own template and a callback to render it.

=head1 CONFIGURATION

In your web application base class, call the C<error_page_config> class method before
any runmode is called.

    __PACKAGE__->error_page_config( %options );

The following configuration options are supported:

=head2 title

An optional string for the error page's <title> tag. If none is supplied, the default,
'Error', will be used.

=head2 css_href

If using the default error page HTML generated by this module, you can use this option 
to specify a relative or absolute URL to a CSS file to customize the look and feel. The 
default HTML uses the following CSS classes:

=over 4

=item err_header

Class for the <h1> element which contains the error title.

=item err_msg

Class for the <div> element which contains the error message.

=back

=head2 render_callback

An optional callback to use your own template rendering function instead of the built-in
default HTML. This option is most useful if using C<catch_rm> (see below.)

    # to use TT
    render_callback => sub { 
        my $self = shift;
        my %args = @_;

        return $self->tt_process( 'error.tmpl', %args );
    }

The C<%args> hash passed to the callback will contain the following keys: 

=over 4

=item title

The text for the <title> tag.

=item msg

The error message.

=back

=head2 catch_rm

If this option is set to true, a special AUTOLOAD subroutine will be exported to your
application module which will intercept any call to a non-existant method (such as 
an invalid runmode) and generate an error page.

=head1 EXPORT

Exports C<error_page> and C<error_page_config> by default.

=head1 AUTHOR

Mike Friedman, C<< <friedo at friedo.com> >>

=head1 BUGS

Please report any bugs or feature requests to
C<bug-cgi-application-plugin-errorpage at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-ErrorPage>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc CGI::Application::Plugin::ErrorPage

You can also look for information at:

=over 4

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/CGI-Application-Plugin-ErrorPage>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/CGI-Application-Plugin-ErrorPage>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Application-Plugin-ErrorPage>

=item * Search CPAN

L<http://search.cpan.org/dist/CGI-Application-Plugin-ErrorPage>

=back

=head1 ACKNOWLEDGEMENTS

=head1 COPYRIGHT & LICENSE

Copyright 2006 Mike Friedman, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/[email protected]/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to