[Catalyst] error handling (Chain where ajax and non-ajax actions
chain off)
David Schmidt
davewood at gmx.at
Tue Nov 9 09:54:58 GMT 2010
Hello list
Both ajax and non-ajax actions chain off the same action
(base_with_id). If an error occurs (like the resource doesn't exist) I
detach to '/error404' in my Root Controller.
I just introduced the ajax stuff and before that 'error404' simply
rendered an error template. Now I'd like to check if it is an xmlhttp
request using http://search.cpan.org/~flora/Catalyst-TraitFor-Request-XMLHttpRequest-0.01/
and then either render the error template like before OR use the
status_not_found() helper from
http://search.cpan.org/~bobtfish/Catalyst-Action-REST-0.87/
To use status_not_found I need to extend my Root Controller from
Catalyst::Controller::REST
That'd mean i have to check the type of request again in my roots end
method to avoid rendering of the template.
#Root.pm
sub render : ActionClass('RenderView') {}
sub end :Private {
my ($self, $c) = @_;
unless ($c->request->is_xhr) {
$c->forward('render');
}
}
Any comments on that solution are appreciated as are pointers to alternatives.
david
#User.pm
sub base : Chained('') PathPart('') CaptureArgs(0) {
my ($self, $c ) = @_;
$c->stash(users_rs => $c->model('DB::Users'));
}
sub base_with_id : Chained('base') PathPart('') CaptureArgs(1) {
my ($self, $c, $id ) = @_;
my $user = $c->stash->{'users_rs'}->find($id);
if ($user) {
$c->stash(user = $user);
} else {
$c->stash(error_msg => 'User not found.');
$c->detach('/error404');
}
}
More information about the Catalyst
mailing list