Skip to content

Commit

Permalink
add better error when object config incomplete
Browse files Browse the repository at this point in the history
When a service config is missing "value" or "config", it must be an
object. If it is then also missing a "class", we get a cryptic error:
"argument is not a module name". This is coming from Module::Runtime
use_module.

Now, if this situation happens, we throw an exception with a better
error message.
  • Loading branch information
preaction committed Feb 29, 2016
1 parent 0aa004d commit 565aaa1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Beam/Wire.pm
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,14 @@ sub create_service {
return $self->_load_config( "$conf_path" );
}

if ( !$service_info{class} ) {
Beam::Wire::Exception::InvalidConfig->throw(
name => $name,
file => $self->file,
error => 'Service configuration incomplete. Missing one of "class", "value", "config"',
);
}

use_module( $service_info{class} );

if ( my $with = $service_info{with} ) {
Expand Down
40 changes: 40 additions & 0 deletions t/method/create_service.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

use strict;
use warnings;
use Test::More;
use Test::Lib;
use Test::Deep;
use Test::Exception;
use Beam::Wire;

my $wire = Beam::Wire->new(
config => {
base_class => {
class => 'My::ArgsTest',
},
base_no_class => { },
},
);

my $svc;

lives_ok { $svc = $wire->create_service( 'testing', class => 'My::ArgsTest' ) }
'create service with class only';
cmp_deeply $svc->got_args, [], 'no args given';

throws_ok { $svc = $wire->create_service( 'testing', path => '/foo/bar' ) }
'Beam::Wire::Exception::InvalidConfig',
'must have one of "class", "value", "config" in the merged config';

throws_ok { $svc = $wire->create_service( 'testing', extends => 'base_no_class' ) }
'Beam::Wire::Exception::InvalidConfig',
'merged config from extends must have one of "class", "value", "config" in the merged config';

throws_ok { $svc = $wire->create_service( 'testing', class => 'My::ArgsTest', value => '' ) }
'Beam::Wire::Exception::InvalidConfig',
'cannot use "value" with "class"';
throws_ok { $svc = $wire->create_service( 'testing', extends => 'base_class', value => '' ) }
'Beam::Wire::Exception::InvalidConfig',
'cannot use "value" with "extends"';

done_testing;

0 comments on commit 565aaa1

Please sign in to comment.