[Dancer-users] Modular approach without anonymous subrefs

Alfie John alfiej at opera.com
Tue Sep 25 02:01:47 CEST 2012


On Tue, Sep 25, 2012, at 09:01 AM, Alfie John wrote:
> sub Handle {
>   my ( $handler ) = @_;
> 
>   set serializer => 'JSON';
> 
>   my $result;
> 
>   return sub {
>     my $user = ...
> 
>     if ( not $user ) {
>       log_stuff();
>       client_error( "User not found or incorrect password", 401 );
>     }
> 
>     var user => $user;
> 
>     other_stuff();
> 
>     $result = $handler->( @_ );
>   };
> 
>   if ( $@ ) {
>     status( 500 );
>     error( $@ );
> 
>     return {
>       Status            => 'Error',
>       ErrorMessage => 'Internal server error',
>     };
>   }
> 
>   return $result;
> }

Oops. Wrongly copied. What I've got is:

  if ( $@ ) {
    if ( ref( $@ ) and $@ =~ /Dancer/ ) {
      $@->rethrow();
    }

    status( 500 );
    error( $@ );

    return {
      Status            => 'Error',
      ErrorMessage => 'Internal server error',
    };
  }

The reasoning here is that if my convenience method client_error() gets
called somewhere in the code, it will eventually call send_error(). This
error handler then gets called which rethrows with the proper client
error message given in client_erro() instead of the default 'Internal
server error'.

Alfie

-- 
  Alfie John
  alfiej at opera.com


More information about the Dancer-users mailing list