-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ::Schema::DidYouMean to help with ->resutlset typos
- Loading branch information
Arthur Axel 'fREW' Schmidt
committed
Nov 2, 2014
1 parent
d9913e3
commit 66e14e1
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |