I'm using Mojolicious for my JSON web-service. In the Documentation I
found the solution for returning customized HTML error page in one of
exception and not found, but since my application has a JSON
interface, I would like to return some customized JSON on this cases.
How do I do this?
Thanks for any help
Best Regards
Marcos
--
Marcos Rebelo
http://www.oleber.com/
Milan Perl Mongers leader https://sites.google.com/site/milanperlmongers/
Webmaster of http://perl5notebook.oleber.com
On Thursday, April 21, 2011, marcos rebelo <ole...@gmail.com> wrote:
> Hy all
>
> I'm using Mojolicious for my JSON web-service. In the Documentation I
> found the solution for returning customized HTML error page in one of
> exception and not found, but since my application has a JSON
> interface, I would like to return some customized JSON on this cases.
>
> How do I do this?
>
> Thanks for any help
>
> Best Regards
> Marcos
>
> --
> Marcos Rebelo
> http://www.oleber.com/
> Milan Perl Mongers leader milanperlmongers <https://sites.google.com/site/milanperlmongers/>
> Webmaster of http://perl5notebook.oleber.com
>
> --
> 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.
>
>
--
С уважением,
Анатолий Шарифулин.
Milan Perl Mongers leader https://sites.google.com/site/milanperlmongers/
Webmaster of http://perl5notebook.oleber.com
--
Sebastian Riedel
http://mojolicio.us
http://twitter.com/kraih
http://blog.kraih.com
The exception page is done by some other method, doesn't passes on
that Controller method.
Best Regards
Marcos
I recently found myself doing things like this :
$Mojolicious::Controller::EXCEPTION =
Mojo::Command->new->get_data( 'exception.html.ep',
"My::Controller::Class" );
It would be nice if these were attributes rather
than package variables, so they could be overridden.
Here's a potential patch :
https://gist.github.com/gists/934522
Brian
--
Sebastian Riedel
http://mojolicio.us
http://twitter.com/kraih
http://blog.kraih.com
Oh, well. Actually, I was doing both (and calling $self->SUPER::render_not_found
in my render_not_found). I suppose I could just move everything into the
derived class.
For some context, I'm not using this technique in a particular application,
but rather in a sub-framework used by a few applications. So, it is just
providing new defaults.
Brian
"render_not_found" is a lot more complicated though since there is the possibility of routes to action-less templates.
Suggestions for how to handle this better or even patches would be very welcome.
(Just remember that there has to be a deprecation path for backwards compatibility breaking changes)
--
Sebastian Riedel
http://mojolicio.us
http://twitter.com/kraih
http://blog.kraih.com
The subject of this email is "Returning a Json Error page on exception
and not found", this means returning a application/json media type,
probably my HTTP-Status, ...
The render exception gives the full liberty/responsibility to the
programmer to decide how to correctly handle the exceptions and the
not found.
Best Regards
Marcos Rebelo
> --
> sebastian
>
> --
> 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.
>
>
--
I do have it in a DATA section, but it looks like Mojolicious::Controller
only looks in the DATA section for Mojolicious::Controller, not in
the DATA section for a derived class.
Brian
Works perfectly, thanks!
Brian
How do I do this???
Thanks for all your help.
Best Regards
Marcos Rebelo
> --
> 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.
>
>
--
$self->render_json({ message => "Help, I'm stuck!" });
-- ams
And for errors, override the controller methods :
package My::Controller;
use base 'Mojolicious::Controller';
sub render_not_found {
shift->render_json({sorry => "not found"});
}
sub render_exception {
shift->render_json({somethings => "wrong"});
}
package main;
use Mojolicious::Lite;
app->controller_class("My::Controller");
...
Brian
--
Ben van Staveren
phone: +62 81 70777529
email: benvans...@gmail.com
Thanks to all
Best Regards
Marcos Rebelo
package MyApp::Controller;
use Mojo::Base 'Mojolicious::Controller';
# overwritten methods of Mojolicious::Controller
sub render_not_found {
$c->respond(404)
}
sub render_exception {
# 1. store & send information about error
# 2. switch to special statuses eg. 408 & 503
# 3. provide more details to json_data_response
$c->respond($status)
}
sub render_not_authorized {
# prepare json_data_response of failed authorization
$c->respond(403)
}
# common method to standarize output for different statuses & formats
sub respond {
# ...
$c->respond_to(
json => sub { # 99% requests are json
$c->render(json => $c->json_data_response)
},
html => sub {
given ($status) {
when (404) { $c->SUPER::render_not_found() }
when (500) { $c->SUPER::render_exception() }
# more eg. 403, 400
}
}
)
}$self->helper('reply.exception_orig' => sub { # make clone of original reply.exception
Mojolicious::Plugin::DefaultHelpers::_development('exception', @_)});$self->helper('reply.exception' => sub { # use customized MyApp::Controller::render_exception
MyApp::Controller::render_exception(@_);});