[dancer-users] Automatic JSON serializer breaks error page

Lee Carmichael leecarmichael123 at gmail.com
Fri Mar 14 13:46:13 GMT 2014


we've had this issue for a while since we run REST services and webapp out
of same code base.

We do this:

hook before_error_init => sub {
    my $err_obj = shift;

    if ( request->path !~ m{^/api/} && ( my $ser = setting('serializer') )
) {
        set 'serializer', undef;
        $err_obj->{_save_serializer} = $ser;
    }
};

And then restore it:

hook after_error_render => sub {
    my $response = shift;
    my $err_obj  = vars->{_save_err_obj};

    if ( my $ser = delete $err_obj->{_save_serializer} ) {
        set 'serializer', $ser;
    }
};

I could see adding a config option to only do this in dev or test
environments since i'd guess in production, you would want to log the error
in JSON.

HTH,

Lee




On Fri, Mar 14, 2014 at 5:20 AM, David Precious <davidp at preshweb.co.uk>wrote:

> On Thu, 13 Mar 2014 18:57:04 -0600
> Warren Young <warren at etr-usa.com> wrote:
>
> > Apparently you cannot have both
> >
> >     serializer: "JSON"
> >     show_errors: 1
> >
> > in your config.yml.  Enabling the automatic JSON serializer defeats
> > the error screen, returning the exception error object as JSON to the
> > client:
> <snip>
>
> You could argue that's correct and desired behaviour - if you have set
> serialiser: "JSON", you are, presumably, writing an API, which will be
> used by API consumers expecting JSON.  Suddenly dumping a HTML error
> page at them instead of the JSON they were expecting would be silly and
> wrong - returning JSON describing the error seems much more sane.
>
> So, I wouldn't say it breaks the error page, I'd say it's logical
> behaviour.
>
> If you don't want it, though, you could probably get round it with
> Yanick's suggestion of using before_serializer.
>
>
> --
> David Precious ("bigpresh") <davidp at preshweb.co.uk>
> http://www.preshweb.co.uk/     www.preshweb.co.uk/twitter
> www.preshweb.co.uk/linkedin    www.preshweb.co.uk/facebook
> www.preshweb.co.uk/cpan        www.preshweb.co.uk/github
>
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20140314/257561b0/attachment.html>


More information about the dancer-users mailing list