Setting a default route for 404s

1,047 views
Skip to first unread message

Jason Dixon

unread,
Nov 28, 2010, 11:50:42 PM11/28/10
to mojol...@googlegroups.com
Is it possible to define a default route for all 404 messages? I'm
building an API service with Mojolicious::Lite and would like to hand
out a json message instead of not_found.html.ep.

Thanks,
Jason

Анатолий Шарифулин

unread,
Nov 29, 2010, 4:31:12 AM11/29/10
to mojol...@googlegroups.com
get '/(*any)' => sub { shift->render_json({error => { code => 404, message => 'Not forund' }}) };



--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To post to this group, send email to mojol...@googlegroups.com.
To unsubscribe from this group, send email to mojolicious...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mojolicious?hl=en.




--
С уважением,
 Анатолий Шарифулин.

Анатолий Шарифулин

unread,
Nov 29, 2010, 4:34:13 AM11/29/10
to mojol...@googlegroups.com
get '/(*any)' => { format => 'json' } => 'not_found';

Jason Dixon

unread,
Nov 29, 2010, 10:41:09 AM11/29/10
to mojol...@googlegroups.com
I had various problems with each of these, but it got me in the right
direction. My final version:

any '/(*)' => sub {
my $self = shift;
$self->res->code(404);
$self->res->message('Not Found');
return $self->render_json({ ErrorCode => 404, Message => 'URI Not Found' });
};

Thanks!
Jason

2010/11/29 Анатолий Шарифулин <shari...@gmail.com>:

Анатолий Шарифулин

unread,
Nov 29, 2010, 10:49:24 AM11/29/10
to mojol...@googlegroups.com
Of course, status code is right (if your app uses REST)
Reply all
Reply to author
Forward
0 new messages