create error_404 action and switch default to use it
[catagits/catbook-code.git] / lib / LolCatalyst / Lite / Controller / Root.pm
1 package LolCatalyst::Lite::Controller::Root;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 __PACKAGE__->config->{namespace} = '';
8
9 sub index :Path :Args(0) {
10     my ( $self, $c ) = @_;
11 }
12
13 sub default :Path {
14     my ( $self, $c ) = @_;
15     $c->detach('/error_404');
16 }
17
18 sub error_404 :Private {
19     my ( $self, $c ) = @_;
20     $c->response->status(404);
21     $c->response->body( 'Page not found' );
22 }
23
24 sub translate :Private {
25      my ($self, $c) = @_;
26      my $lol = $c->req->body_params->{lol}; # only for a POST request
27          # $c->req->params->{lol} would catch GET or POST
28          # $c->req->query_params would catch GET params only
29      $c->stash(
30        lol => $lol,
31        result => $c->model('Translator')->translate($lol),
32        template => 'index.tt',
33      );
34 }
35
36 sub translate_service : Local {
37     my ($self, $c) = @_;
38     $c->forward('translate');
39     $c->stash->{current_view} = 'Service';
40 }
41
42 sub end : ActionClass('RenderView') {
43     my ($self, $c) = @_;
44     my $errors = scalar @{$c->error};
45     if ($errors) {
46         $c->log->error("Errors in ${\$c->action}:");
47         $c->log->error($_) for @{$c->error};
48         $c->res->status(500);
49         $c->res->body('internal server error');
50         $c->clear_errors;
51     }
52 }
53
54 1;