Skip to content

Commit

Permalink
fix error reporting when loading applications with compile errors
Browse files Browse the repository at this point in the history
When switching from loading an application from using require to using
do, the error was still trying to be captured after a surrounding eval.
This error would always be empty, because the error from the do was not
rethrown. It would still result in an undef application, so an error
would be thrown, but it would not include the actual compile error.

Change the do call inside the eval to throw an error if no application
is returned. The error is then recaught outside the eval. Also add a
test that shows that the real compilation error is included in the
output.

Fixes mojolicious#2110
  • Loading branch information
haarg committed Sep 19, 2023
1 parent 4cf8bab commit 852a759
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/Mojo/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ sub load_app {
local @ARGS_OVERRIDE = @args;

# Try to load application from script into sandbox
my $app = eval "package Mojo::Server::Sandbox::@{[md5_sum $path]}; do \$path";
my $err = $app ? undef : $@ || $! || "$path did not return a true value";
die qq{Can't load application from file "$path": $err} if $err;
my $app = eval sprintf <<'END_CODE', md5_sum($path);
package Mojo::Server::Sandbox::%s;
do $path or die $@ || $! || "$path did not return a true value";
END_CODE
die qq{Can't load application from file "$path": $@} if $@;
die qq{File "$path" did not return an application object.\n} unless blessed $app && $app->can('handler');
$self->app($app);
};
Expand Down
3 changes: 2 additions & 1 deletion t/mojo/daemon.t
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ subtest 'Load broken app' => sub {
eval { Mojo::Server::Daemon->new->load_app("$bin/lib/Mojo/LoaderTest/A.pm") };
like $@, qr/did not return an application object/, 'right error';
eval { Mojo::Server::Daemon->new->load_app("$bin/lib/Mojo/LoaderException.pm") };
like $@, qr/^Can't load application/, 'right error';
like $@, qr/Missing right curly or square bracket/, 'right error';
like $@, qr/^Can't load application/, 'right error';
};

subtest 'Load app using module_true' => sub {
Expand Down

0 comments on commit 852a759

Please sign in to comment.