Skip to content

Commit

Permalink
Add ::Schema::DidYouMean to help with ->resutlset typos
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Axel 'fREW' Schmidt committed Nov 2, 2014
1 parent d9913e3 commit 66e14e1
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
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::Schema::DidYouMean to help with typos when calling ->resultset

2.023007 2014-09-16 19:48:34-05:00 America/Chicago
- Fix ::Helper::ResultSet::DateMethods1 for Oracle (thanks Alexander Hartmaier!)
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ requires 'DBIx::Introspector' => 0.001002;
requires 'Module::Runtime';
requires 'Try::Tiny';
requires 'Safe::Isa';
requires 'Text::Brew';

on test => sub {
requires 'Test::More' => 0.94;
Expand Down
67 changes: 67 additions & 0 deletions lib/DBIx/Class/Helper/Schema/DidYouMean.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package DBIx::Class::Helper::Schema::DidYouMean;

# ABSTRACT: Nice error messages when you misspell the name of a ResultSet

use strict;
use warnings;

use Text::Brew 'distance';
use Try::Tiny;
use namespace::clean;

sub resultset {
my ($self, @rest) = @_;

my $method = $self->next::can;

try {
$self->$method(@rest)
} catch {
if (m/Can't find source for (.+?) at/) {
my @presentsources = map {
(distance($_, $1))[0] < 3 ? " * $_ <-- Possible Match\n" : " $_\n";
} sort $self->storage->schema->sources;

die <<"ERR";
$_
The ResultSet "$1" is not part of your schema.
To help you debug this issue, here's a list of the actual sources that the
schema knows about:
@presentsources
ERR
}
die $_;
}
}

1;

=head1 SYNOPSIS
package MyApp::Schema;
__PACKAGE__->load_components('Helper::Schema::DidYouMean');
Elsewhere:
$schema->resultset('Usre')->search(...)->...
And a nice exception gets thrown:
The ResultSet "Usre" is not part of your schema.
To help you debug this issue, here's a list of the actual sources that the
schema knows about:
Account
Permission
Role
* User <-- Possible Match
=head1 DESCRIPTION
This helper captures errors thrown when you use the C<resultset> method on your
schema and typo the source name. It tries to highlight the best guess as to
which you meant to type.
1 change: 1 addition & 0 deletions t/lib/TestSchema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ __PACKAGE__->load_namespaces(
__PACKAGE__->load_components(qw(
Helper::Schema::LintContents
Helper::Schema::QuoteNames
Helper::Schema::DidYouMean
));

sub upgrade_directory { './t/lib' }
Expand Down
20 changes: 20 additions & 0 deletions t/schema/did-you-mean.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!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('foo_Bar') },
qr/\* Foo_Bar <--/,
'found correct RS',
);

done_testing;

0 comments on commit 66e14e1

Please sign in to comment.