Skip to content

Commit

Permalink
Greatly improve diagnostic messages of _resolve_relationship_condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ribasushi committed Jul 20, 2014
1 parent e84ae5d commit 209a206
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions lib/DBIx/Class/ResultSource.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1782,16 +1782,19 @@ sub _resolve_relationship_condition {
my $args = { ref $_[0] eq 'HASH' ? %{ $_[0] } : @_ };

for ( qw( rel_name self_alias foreign_alias ) ) {
$self->throw_exception("Mandatory argument '$_' is not a plain string")
$self->throw_exception("Mandatory argument '$_' to _resolve_relationship_condition() is not a plain string")
if !defined $args->{$_} or length ref $args->{$_};
}

$self->throw_exception('No practical way to resolve a relationship between two objects')
if defined $args->{self_resultobj} and defined $args->{foreign_resultobj};

my $rel_info = $self->relationship_info($args->{rel_name});
# or $self->throw_exception( "No such relationship '$args->{rel_name}'" );

my $exception_rel_id = "relationship '$args->{rel_name}' on source '@{[ $self->source_name ]}'";

$self->throw_exception("No practical way to resolve $exception_rel_id between two objects")
if defined $args->{self_resultobj} and defined $args->{foreign_resultobj};


$self->throw_exception( "Object '$args->{foreign_resultobj}' must be of class '$rel_info->{class}'" )
if defined blessed $args->{foreign_resultobj} and ! $args->{foreign_resultobj}->isa($rel_info->{class});

Expand Down Expand Up @@ -1832,7 +1835,7 @@ sub _resolve_relationship_condition {
if (my $jfc = $ret->{join_free_condition}) {

$self->throw_exception (
"The join-free condition returned for relationship '$args->{rel_name}' must be a hash reference"
"The join-free condition returned for $exception_rel_id must be a hash reference"
) unless ref $jfc eq 'HASH';

my ($joinfree_alias, $joinfree_source);
Expand All @@ -1847,7 +1850,7 @@ sub _resolve_relationship_condition {

# FIXME sanity check until things stabilize, remove at some point
$self->throw_exception (
"A join-free condition returned for relationship '$args->{rel_name}' without a result object to chain from"
"A join-free condition returned for $exception_rel_id without a result object to chain from"
) unless $joinfree_alias;

my $fq_col_list = { map
Expand All @@ -1856,7 +1859,7 @@ sub _resolve_relationship_condition {
};

$fq_col_list->{$_} or $self->throw_exception (
"The join-free condition returned for relationship '$args->{rel_name}' may only "
"The join-free condition returned for $exception_rel_id may only "
. 'contain keys that are fully qualified column names of the corresponding source'
) for keys %$jfc;

Expand Down Expand Up @@ -1956,10 +1959,10 @@ sub _resolve_relationship_condition {
}
}
else {
$self->throw_exception ("Can't handle condition $args->{condition} for relationship '$args->{rel_name}' yet :(");
$self->throw_exception ("Can't handle condition $args->{condition} for $exception_rel_id yet :(");
}

$self->throw_exception("Relationship '$args->{rel_name}' does not resolve to a join-free condition fragment") if (
$self->throw_exception(ucfirst "$exception_rel_id does not resolve to a join-free condition fragment") if (
$args->{require_join_free_condition}
and
( ! $ret->{join_free_condition} or $ret->{join_free_condition} eq UNRESOLVABLE_CONDITION )
Expand Down Expand Up @@ -1994,8 +1997,7 @@ sub _resolve_relationship_condition {
if ($args->{infer_values_based_on}) {

$self->throw_exception(sprintf (
"Unable to complete value inferrence - custom relationship '%s' returns conditions instead of values for column(s): %s",
$args->{rel_name},
"Unable to complete value inferrence - custom $exception_rel_id returns conditions instead of values for column(s): %s",
map { "'$_'" } @nonvalues
)) if @nonvalues;

Expand Down
4 changes: 2 additions & 2 deletions t/relationship/custom.t
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ lives_ok {
# try to create_related a 80s cd
throws_ok {
$artist->create_related('cds_80s', { title => 'related creation 1' });
} qr/\QUnable to complete value inferrence - custom relationship 'cds_80s' returns conditions instead of values for column(s): 'year'/,
} qr/\QUnable to complete value inferrence - custom relationship 'cds_80s' on source 'Artist' returns conditions instead of values for column(s): 'year'/,
'Create failed - complex cond';

# now supply an explicit arg overwriting the ambiguous cond
Expand All @@ -182,7 +182,7 @@ is(
# try a specific everything via a non-simplified rel
throws_ok {
$artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
} qr/\QRelationship 'cds_90s' does not resolve to a join-free condition fragment/,
} qr/\QRelationship 'cds_90s' on source 'Artist' does not resolve to a join-free condition fragment/,
'Create failed - non-simplified rel';

# Do a self-join last-entry search
Expand Down

0 comments on commit 209a206

Please sign in to comment.