From: Colin Newell Date: Fri, 8 Jan 2016 23:06:42 +0000 (+0000) Subject: Ensure Module::Runtime use_package_optimistically works. X-Git-Tag: v0.003006~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=daa3054c043a622b2dd27f3176436e802bcd910c;p=scpubgit%2FObject-Remote.git Ensure Module::Runtime use_package_optimistically works. Catches and re-throws errors to allow us to grab the information it is expecting. --- diff --git a/Makefile.PL b/Makefile.PL index 745aaaf..6fe8cef 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -10,6 +10,7 @@ WriteMakefile( PREREQ_PM => { Moo => 1.006, 'Module::Runtime' => 0, + 'Try::Tiny' => 0, 'JSON::PP' => 0, 'Future' => 0.29, 'MRO::Compat' => 0, # required to fatpack Moo diff --git a/lib/Object/Remote/ModuleLoader.pm b/lib/Object/Remote/ModuleLoader.pm index aff3d56..9d4273e 100644 --- a/lib/Object/Remote/ModuleLoader.pm +++ b/lib/Object/Remote/ModuleLoader.pm @@ -4,19 +4,36 @@ BEGIN { package Object::Remote::ModuleLoader::Hook; use Moo; use Object::Remote::Logging qw( :log :dlog ); + use Try::Tiny; has sender => (is => 'ro', required => 1); # unqualified INC forced into package main sub Object::Remote::ModuleLoader::Hook::INC { my ($self, $module) = @_; log_debug { "Loading $module via " . ref($self) }; - if (my $code = $self->sender->source_for($module)) { - open my $fh, '<', \$code; - Dlog_trace { "Module sender successfully sent code for '$module': $code" } $code; - return $fh; + try + { + if (my $code = $self->sender->source_for($module)) { + open my $fh, '<', \$code; + Dlog_trace { "Module sender successfully sent code for '$module': $code" } $code; + return $fh; + } + log_trace { "Module sender did not return code for '$module'" }; + return; + } + catch + { + log_trace { "Module sender blew up - $_" }; + if($_ =~ /Can't locate/) + { + # Fudge the error messge to make it work with + # Module::Runtime use_package_optimistically + # Module::Runtime wants - /\ACan't locate \Q$fn\E .+ at \Q@{[__FILE__]}\E line/ + my ($package, $file, $line) = caller(9); + s/(in \@INC.)/$1 at $file line $line/; + } + die $_; } - log_trace { "Module sender did not return code for '$module'" }; - return; } } diff --git a/xt/load_optional.t b/xt/load_optional.t index 2e7f510..fda12a2 100644 --- a/xt/load_optional.t +++ b/xt/load_optional.t @@ -21,7 +21,10 @@ is exception { my $remote = My::Data::TestModuleRuntime->new::on($connection); is($remote->counter, 0, 'Counter at 0'); is($remote->increment, 1, 'Increment to 1'); - is($remote->has_missing_module, 0, 'Shouldn\'t have loaded module'); + like exception { + my $o = $remote->create_object; + }, qr/Can't locate Not\/Found.pm in \@INC/, 'Should fail to load Not::Found'; + }, undef, 'Checking Module::Runtime use_package_optimistically works correctly.'; done_testing; @@ -51,4 +54,4 @@ has counter => (is => 'rwp', default => sub { 0 }); sub increment { $_[0]->_set_counter($_[0]->counter + 1); } -sub has_missing_module { HAS_MISSING_MODULE }; +sub create_object { use_package_optimistically('Not::Found')->new() };