Skip to content
This repository
Browse code

Add warnings when ->to_app implicit promotion is done.

Usually it's developer's mistake where they forgot ->to_app after
Plack::App::Foo->new(). This patch gives warnings in such case in
the development environment.

This will probably give lots of warnings with the command line use
such as:

  plackup -MPlack::App::File -e 'Plack::App::File->new'

which i will work around with a separate commit.
  • Loading branch information...
commit b2455e1cfc3f1a8f9d91479cc068b6510ff1c8cc 1 parent 4b2b1ba
Tatsuhiko Miyagawa miyagawa authored

Showing 1 changed file with 12 additions and 1 deletion. Show diff stats Hide diff stats

  1. +12 1 lib/Plack/Component.pm
13 lib/Plack/Component.pm
@@ -3,7 +3,7 @@ use strict;
3 3 use warnings;
4 4 use Carp ();
5 5 use Plack::Util;
6   -use overload '&{}' => sub { shift->to_app(@_) }, fallback => 1;
  6 +use overload '&{}' => \&to_app_auto, fallback => 1;
7 7
8 8 sub new {
9 9 my $proto = shift;
@@ -19,6 +19,17 @@ sub new {
19 19 $self;
20 20 }
21 21
  22 +sub to_app_auto {
  23 + my $self = shift;
  24 + if (($ENV{PLACK_ENV} || '') eq 'development') {
  25 + my $class = ref($self);
  26 + warn "WARNING: Automatically converting $class instance to a PSGI code reference. " .
  27 + "If you see this warning for each request, you probably need to explicitly call " .
  28 + "to_app() i.e. $class->new(...)->to_app in your PSGI file.\n";
  29 + }
  30 + $self->to_app(@_);
  31 +}
  32 +
22 33 # NOTE:
23 34 # this is for back-compat only,
24 35 # future modules should use

0 comments on commit b2455e1

Please sign in to comment.
Something went wrong with that request. Please try again.