I've made asap workaround that works with my old solution:

$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(@_);
});





W dniu poniedziałek, 13 kwietnia 2015 09:17:00 UTC+2 użytkownik njzt 
napisał:
>
> Thanks, I will try with that.
>
> W dniu piątek, 10 kwietnia 2015 14:19:31 UTC+2 użytkownik Jan Henning 
> Thorsen napisał:
>>
>> I'm doing this:
>>
>>   $self->hook(
>>     before_render => sub {
>>       return unless $_[0]->accepts('json');
>>       my ($c, $args) = @_;
>>       my $template = $args->{template} || '';
>>
>>       if ($template =~ /^not_found/) {
>>         $args->{json} = {errors => [{path => '/', message => 'Resource 
>> not found.'}]};
>>       }
>>       elsif ($template =~ /^exception/) {
>>         $args->{json} = {errors => [{path => '/', message => 
>> $args->{exception} || 'Unknown error.'}]};
>>       }
>>     }
>>   );
>>
>> Have a look here for more details:
>>
>> https://metacpan.org/pod/distribution/Mojolicious/lib/Mojolicious/Guides/Rendering.pod#Rendering-exception-and-not_found-pages
>>
>> On Friday, April 10, 2015 at 2:07:28 PM UTC+2, njzt wrote:
>>>
>>> I have quite similar case in  my JSON REST app. Using Mojolicious 5 I 
>>> was overriding render_exception and render_not_found in my main Controller 
>>> class. Here is a short version:
>>>
>>> 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
>>>       }
>>>     }
>>>   )
>>> }
>>>
>>> Do you have any suggestion how can I easily migrate such solution to 
>>> Mojolicious 6 where render_exception and render_not_found are no longer 
>>> supported. I was thinking of overriding reply.exception and reply.not_found 
>>> helpers. Any other ideas?
>>>
>>> W dniu czwartek, 21 kwietnia 2011 08:32:26 UTC+2 użytkownik oleber 
>>> napisał:
>>>>
>>>> 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 
>>>> 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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to