Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Custom error handling

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


kakimoto at tpg

Jul 3, 2009, 12:33 AM

Post #1 of 7 (662 views)
Permalink
Custom error handling

hi ,guys,

referring to
http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/Cookbook.pod#Delivering_a_Custom_Error_Page,
i can't seem to see the error page when
I have my Root.pm's sub end set up this way.



sub end : ActionClass('RenderView') {
my ($self, $c) = @_;
$c->log->debug(" Calling END in Root ");

if ( scalar @{ $c->error } ) {
$c->stash->{errors} = $c->error;
$c->stash->{template} = 'menu.tt';
$c->error(0);
}
else
{
$c->log->debug(" nothign done ");
}
}


I can see that a $c->error () is being called in one of my controllers
and the Room->end has been called (thanks to my debugging messages
above) BUT the $c->error doesn't seem to be detected in the Room->end
that I have. Instead, i see "nothign done" being printed out.


Any ideas , fellas?

thank you:)


K. akimoto

_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jul 3, 2009, 1:15 AM

Post #2 of 7 (618 views)
Permalink
Re: Custom error handling [In reply to]

On 3 Jul 2009, at 08:33, kakimoto[at]tpg.com.au wrote:
>
> I can see that a $c->error () is being called in one of my controllers

You mean you're calling $c->error yourself in your own controller code?

> and the Room->end has been called (thanks to my debugging messages
> above) BUT the $c->error doesn't seem to be detected in the Room->end
> that I have. Instead, i see "nothign done" being printed out.

You haven't shown us the code you're putting stuff into $c->error
with, therefore it's pretty hard to tell how you're doing it wrong.

Try just putting:

die("An error")

into your controller code, and you should see that captured in $c-
>error when it gets to your end action

Cheers
t0m


_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


kakimoto at tpg

Jul 4, 2009, 9:32 PM

Post #3 of 7 (596 views)
Permalink
Re: Custom error handling [In reply to]

hello, Tomas,

thank you :) My replies are as per below.


> You mean you're calling $c->error yourself in your own controller
> code?

Yes I am.
> You haven't shown us the code you're putting stuff into $c->error
> with, therefore it's pretty hard to tell how you're doing it wrong.


---- Extract from Controller (Start) ----
sub _save_address
{
};
if ($@)
{
$c->log->debug('Exception while saving address - '. $@);

$c->error(
q{ An error has occured with the address entered. Please
ensure } .
q{that you have entered a valid address. If this persists, } .
q{please contact our helpdesk at helpdesk[at]loadproperty.com.au.}
);
}

return $address->id;
}

---- Extract from Controller (End) ----

>
> Try just putting:
>
> die("An error")
>
> into your controller code, and you should see that captured in $c-
> >error when it gets to your end action
>
yes, when i did that in my controller code, this is what I saw on the
terminal's logs:

[error] Caught exception in myApp::Controller::Listings->_save_address
"An error at
/home/kakimoto/projects/myApp/script/../lib/myApp/Controller/Listings.pm
line 590."



When I remove "-Debug" from myApp.pm, I get just the generic screen
saying "Please come back later" whilst when I have "-Debug" there in
myApp.pm, I get a screen with the following message in the web browser.

"Caught exception in myApp::Controller::Listings->_save_address "An
error at
/home/kakimoto/projects/myApp/script/../lib/myApp/Controller/Listings.pm
line 590."


My Root->end now looks like this:


sub end : ActionClass('RenderView') {
my ($self, $c) = @_;

$c->log->debug(" Calling END in Root "
#. Data::Dumper->Dump([$c->error])
);

if ( scalar @{ $c->error } ) {
$c->log->debug(' Found an error in Root' );
$c->stash->{errors} = $c->error;
$c->stash->{template} = 'menu.tt';
}
else
{
$c->log->debug(" nothing done ");
}
return 1;
}


In the terminal logs, I do get the message, "Found an error in Root"
BUT it doesn't load the template i specified (menu.tt2).


Any idea of how to get a custom screen up?



_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jul 5, 2009, 7:22 AM

Post #4 of 7 (590 views)
Permalink
Re: Custom error handling [In reply to]

On 5 Jul 2009, at 05:32, kakimoto[at]tpg.com.au wrote:
> Any idea of how to get a custom screen up?

You're not clearing the errors, so Catalyst gives you an error screen:

http://search.cpan.org/~flora/Catalyst-Runtime-5.80007/lib/
Catalyst.pm#$c-%3Eclear_errors

Cheers
t0m


_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


kakimoto at tpg

Jul 5, 2009, 6:56 PM

Post #5 of 7 (586 views)
Permalink
Re: Custom error handling [In reply to]

hello Tomas and everyone,

Thank you for that.

I got it working.


What I also did was the in the case of an exception, I will redirect
the user back to the main page with the error shown.

I would like to share what I did.

On Root.pm->end: I passed the error message to the flash (I have
flash_to_stash turned on) because due to a redirection, the only way the
error message can be sustained is via the flash.
============


sub end : ActionClass('RenderView') {
my ($self, $c) = @_;

if ( scalar @{ $c->error } ) {
$c->flash->{errors} = $c->error;

$c->response->redirect($c->uri_for('/'));
$c->error(0);
}
return 1;
};


On my controller, Subscriptions.pm
=======================

sub _save_address : Private
{
eval
{
.....
};
if ($@)
{
$c->error (' A problem occurred when saving your address.
Please try again later' );
}
}



Using die(' A problem occurred when saving your address. Please try
again later' );,
I would get an error message such as "Exception caught in
myApp::Controller::Subscription->_save_address" which doesn't look so
good to the users.
that's why I used $c->error.

Please tell me if it's bad practice?


thank you, everybody for your time! :)


K. akimoto






On Mon, Jul 6th, 2009 at 12:22 AM, Tomas Doran <bobtfish[at]bobtfish.net>
wrote:

>
> On 5 Jul 2009, at 05:32, kakimoto[at]tpg.com.au wrote:
> > Any idea of how to get a custom screen up?
>
> You're not clearing the errors, so Catalyst gives you an error
> screen:
>
> http://search.cpan.org/~flora/Catalyst-Runtime-5.80007/lib/
> Catalyst.pm#$c-%3Eclear_errors
>
> Cheers
> t0m
>
>
> _______________________________________________
> List: Catalyst[at]lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>
>




_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jul 6, 2009, 1:22 AM

Post #6 of 7 (579 views)
Permalink
Re: Custom error handling [In reply to]

On 6 Jul 2009, at 02:56, kakimoto[at]tpg.com.au wrote:
>
> Using die(' A problem occurred when saving your address. Please try
> again later' );,
> I would get an error message such as "Exception caught in
> myApp::Controller::Subscription->_save_address" which doesn't look so
> good to the users.
> that's why I used $c->error.
>
> Please tell me if it's bad practice?

No, what you're doing is fine and supported, but I generally arrange
to throw exception objects (instead of strings).

These don't get in any way mangled by Catalyst, allowing you to
fiddle with them in end to do things such as internationalizing them,
or serializing them in API interfaces, and also means that I have
less flow control to deal with in exceptional cases..

But throwing strings around is however a perfectly valid technique.

Cheers
t0m


_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


kakimoto at tpg

Jul 6, 2009, 2:22 AM

Post #7 of 7 (577 views)
Permalink
Re: Custom error handling [In reply to]

hello Tomas,

thank you. I will look into fine tuning it in the future. Will read more.

K. akimoto

>
> But throwing strings around is however a perfectly valid technique.
>



_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.