Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved exception handling of application and config file loaders
  • Loading branch information
kraih committed May 5, 2012
1 parent 6ff17ab commit 286403f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -4,6 +4,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Added remove method to Mojolicious::Routes::Route.
- Improved 32bit Perl support of Mojo::Transaction::WebSocket.
(mikemagowan, sri)
- Improved exception handling of application and config file loaders.
- Improved renderer log messages.
- Improved documentation.
- Improved tests.
Expand Down
18 changes: 7 additions & 11 deletions lib/Mojo/Server.pm
Expand Up @@ -31,20 +31,16 @@ sub load_app {
local ($ENV{MOJO_APP}, $ENV{MOJO_EXE});

# Try to load application from script into sandbox
my $class = 'Mojo::Server::SandBox::' . md5_sum($file . $$);
my $app;
die $@ unless eval <<EOF;
package $class;
my $app = eval <<EOF;
package Mojo::Server::SandBox::@{[md5_sum($file . $$)]};
{
unless (\$app = do \$file) {
die qq/Can't load application "\$file": \$@/ if \$@;
die qq/Can't load application "\$file": \$!/ unless defined \$app;
die qq/Can't load application' "\$file".\n/ unless \$app;
}
my \$app = do \$file;
if (!\$app && (my \$e = \$@ || \$!)) { die \$e }
return \$app;
}
1;
EOF
die qq/"$file" is not a valid application.\n/
die qq/Couldn't load application from file "$file": $@/ if !$app && $@;
die qq/File "$file" did not return an application object.\n/
unless blessed $app && $app->isa('Mojo');
return $self->app($app)->app;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Plugin/Config.pm
Expand Up @@ -23,9 +23,9 @@ sub parse {
my ($self, $content, $file, $conf, $app) = @_;

# Run Perl code
no warnings;
die qq/Couldn't parse config file "$file": $@/
unless my $config = eval "sub app { \$app }; $content";
my $config = eval 'package Mojolicious::Plugin::Config::Sandbox;'
. "{ no warnings; sub app { \$app }; use Mojo::Base -strict; $content }";
die qq/Couldn't load configuration from file "$file": $@/ if !$config && $@;
die qq/Config file "$file" did not return a hash reference.\n/
unless ref $config eq 'HASH';

Expand Down

0 comments on commit 286403f

Please sign in to comment.