From 2d1409f7dd94e2292690eb03d7d62e464c1714db Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Tue, 21 Dec 2004 20:37:49 +0000 Subject: [PATCH] + fix #7917 - use correct template if object creation fails + fix #7930 - handle Class::DBI constraint failures + assign bug id to relevant changes git-svn-id: http://svn.maypole.perl.org/Maypole/trunk@320 48953598-375a-da11-a14b-00016c27c3ee --- Changes | 12 ++++++++---- ex/BeerDB.pm | 2 +- lib/Maypole/Model/CDBI.pm | 33 ++++++++++++++++++++++----------- lib/Maypole/View/Base.pm | 3 ++- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/Changes b/Changes index 2fee5bc..7444732 100644 --- a/Changes +++ b/Changes @@ -16,13 +16,17 @@ Revision history for Perl extension Maypole (Dagfinn Ilmari Mannsåker) - Added $r->headers_in & $r->headers_out (request & response headers) - Split the meat of the view template off to a view_item macro. - - Prefix warn() in M::V::Base with error description + - #7643 - improve M::V::Base's error() messages. prefix error with label, + and carp() - #7651 - minor POD improvements in M::M::CDBI (Dave Howorth) - #7834 - minor POD update in M::M::CDBI (Kevin Connor) - default search action removes empty fields from search parameters - - Maypole::Application will create a separate Maypole::Config for each - application (instead of the current workaround for multiple apps under - mod_perl) + - #6622 - Maypole::Application will create a separate Maypole::Config for + each application (instead of the current workaround for multiple apps + under mod_perl) + - #7917 - if do_edit fails for object creation, try again with the correct + template (addnew) + - #7930 - handle Class::DBI constraint failures in do_edit. - classmetadata template variables can now be overriden individually - Template toolkit objects are configured with Maypole::Config->view_options - Make test suite work with DBD::SQLite2 diff --git a/ex/BeerDB.pm b/ex/BeerDB.pm index e276830..6d90b2e 100644 --- a/ex/BeerDB.pm +++ b/ex/BeerDB.pm @@ -16,7 +16,7 @@ BEGIN { unless -e DATASOURCE; eval "require DBD::SQLite"; if ($@) { - eval "require DBD::SQLite2" && dbi_driver = 'SQLite2'; + eval "require DBD::SQLite2" and $dbi_driver = 'SQLite2'; } } BeerDB->setup(join ':', "dbi", $dbi_driver, DATASOURCE); diff --git a/lib/Maypole/Model/CDBI.pm b/lib/Maypole/Model/CDBI.pm index 2d30990..37b1335 100644 --- a/lib/Maypole/Model/CDBI.pm +++ b/lib/Maypole/Model/CDBI.pm @@ -97,27 +97,38 @@ sub do_edit : Exported { my $h = CGI::Untaint->new( %{ $r->{params} } ); my $creating = 0; my ($obj) = @{ $r->objects || [] }; + my $fatal; if ($obj) { - # We have something to edit - $obj->update_from_cgi( $h => - { required => $r->{config}{ $r->{table} }{required_cols} || [], } - ); + eval { + $obj->update_from_cgi( $h => + { required => $r->{config}{ $r->{table} }{required_cols} || [], } + ); + }; + $fatal = $@; } else { - $obj = - $self->create_from_cgi( $h => - { required => $r->{config}{ $r->{table} }{required_cols} || [], } - ); + eval { + $obj = + $self->create_from_cgi( $h => + { required => $r->{config}{ $r->{table} }{required_cols} || [], } + ); + }; + $fatal = $@; $creating++; } - if ( my %errors = $obj->cgi_update_errors ) { + if ( my %errors = $fatal ? (FATAL => $fatal) : $obj->cgi_update_errors ) { # Set it up as it was: $r->{template_args}{cgi_params} = $r->{params}; $r->{template_args}{errors} = \%errors; - $r->{template} = "edit"; - undef $obj if $creating; # Couldn't create + + if ($creating) { + undef $obj; + $r->template("addnew"); + } else { + $r->template("edit"); + } } else { $r->{template} = "view"; diff --git a/lib/Maypole/View/Base.pm b/lib/Maypole/View/Base.pm index c56ee51..6c6b63c 100644 --- a/lib/Maypole/View/Base.pm +++ b/lib/Maypole/View/Base.pm @@ -3,6 +3,7 @@ use File::Spec; use UNIVERSAL::moniker; use strict; use Maypole::Constants; +use Carp; sub new { bless {}, shift } # By default, do nothing. @@ -74,7 +75,7 @@ sub process { sub error { my ( $self, $r, $desc ) = @_; $desc = $desc ? "$desc: " : ""; - warn $desc . $r->{error} ."\n"; + carp $desc . $r->{error}; if ( $r->{error} =~ /not found$/ ) { # This is a rough test to see whether or not we're a template or -- 2.39.5