[dancer-users] Automatic JSON serializer breaks error page

Warren Young warren at etr-usa.com
Mon Mar 24 15:09:39 GMT 2014


On 3/14/2014 07:46, Lee Carmichael wrote:
> 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;
>      }
> };

After using this for a while, I noticed that after an error happens, the 
serializer setting isn't getting restored.  This simpler rewrite appears 
to have the same effect, without the problem:

     hook before_error_init => sub {
         set('serializer', undef) if request->path !~ m{^/api/};
     };
     hook after_error_render => sub {
         set('serializer', 'JSON') unless defined setting('serializer');
     };

The restoration code is heavy-handed and violates the DRY principle, but 
I think it will work for our purposes.


More information about the dancer-users mailing list