Skip to content

Commit

Permalink
Add ::ResultSet::Errors to help when calling Result methods on an RS
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Axel 'fREW' Schmidt committed Feb 21, 2015
1 parent 9e22e03 commit e24db2f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for {{$dist->name}}

{{$NEXT}}
- Add ::Helper::ResultSet::Errors to help when calling Result methods on an RS

2.024001 2014-11-25 19:16:41-06:00 America/Chicago
- Fix ::DidYouMean to override source instead of resultset (Thanks ribasushi
Expand Down
58 changes: 58 additions & 0 deletions lib/DBIx/Class/Helper/ResultSet/Errors.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package DBIx::Class::Helper::ResultSet::Errors;

use strict;
use warnings;

my $std_err = qq{Can't locate object method "%s" via package "%s" } .
qq{at %s line %d.\n};

my $cust_err = qq{You're trying to call a Result ("%s") method ("%s") } .
qq{on a ResultSet ("%s") at %s line %d.\n};

sub AUTOLOAD {
my $self = shift;

my($class) = ref $self || $self;

my($meth) = $DBIx::Class::Helper::ResultSet::Errors::AUTOLOAD
=~ m/::([^:]+)$/;

return if $meth eq 'DESTROY';

my($callpack, $callfile, $callline) = caller;

my $rclass = $self->result_source->result_class;

die sprintf $cust_err, $rclass, $meth, $class, $callfile, $callline
if $rclass->can($meth);

die sprintf $std_err, $meth, $class, $callfile, $callline;
}

1;

__END__
=pod
=head1 SYNOPSIS
package MyApp::Schema::ResultSet::Foo;
__PACKAGE__->load_components(qw{Helper::ResultSet::Errors});
...
1;
And then in a script or something:
my $col = $rs->id
# dies with a helpful error!
=head1 DESCRIPTION
Users new to C<DBIx::Class> often make the mistake of treating ResultSets like
Results. This helper ameliorates the situation by giving a helpful error when
the user calls methods for the result on the ResultSet.
18 changes: 18 additions & 0 deletions t/ResultSet/Errors.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!perl

use strict;
use warnings;

use lib 't/lib';
use Test::More;
use Test::Fatal;

use TestSchema;
my $schema = TestSchema->deploy_or_connect();

like exception {
$schema->resultset('Gnarly')->literature
}, qr{^\QYou're trying to call a Result ("TestSchema::Result::Gnarly") method ("literature") on a ResultSet ("TestSchema::ResultSet::Gnarly") at t/ResultSet/Errors.t line 14.\E}, 'exceptional';

done_testing;

2 changes: 1 addition & 1 deletion t/lib/TestSchema/ResultSet/Gnarly.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use warnings;
# intentionally not using TestSchema::ResultSet
use parent 'DBIx::Class::ResultSet';

__PACKAGE__->load_components(qw{ Helper::ResultSet::Me Helper::ResultSet::ResultClassDWIM Helper::ResultSet::CorrelateRelationship Helper::ResultSet::SearchOr Helper::ResultSet::NoColumns Helper::ResultSet::Explain });
__PACKAGE__->load_components(qw{ Helper::ResultSet::Me Helper::ResultSet::ResultClassDWIM Helper::ResultSet::CorrelateRelationship Helper::ResultSet::SearchOr Helper::ResultSet::NoColumns Helper::ResultSet::Explain Helper::ResultSet::Errors });

sub with_id_plus_one {
my $self = shift;
Expand Down

0 comments on commit e24db2f

Please sign in to comment.