File Coverage

blib/lib/DBIx/Class/Helper/Schema/DidYouMean.pm
Criterion Covered Total %
statement 26 27 96.3
branch 3 4 75.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 39 41 95.1


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::Schema::DidYouMean;
2             $DBIx::Class::Helper::Schema::DidYouMean::VERSION = '2.036000';
3             # ABSTRACT: Nice error messages when you misspell the name of a ResultSet
4              
5 56     56   26065 use strict;
  56         147  
  56         1571  
6 56     56   308 use warnings;
  56         117  
  56         1451  
7              
8 56     56   277 use parent 'DBIx::Class::Schema';
  56         116  
  56         276  
9              
10 56     56   27633 use Text::Brew 'distance';
  56         77876  
  56         3896  
11 56     56   434 use Try::Tiny;
  56         124  
  56         2515  
12 56     56   321 use namespace::clean;
  56         122  
  56         421  
13              
14             sub source {
15 1874     1874 1 8315998 my ($self, @rest) = @_;
16              
17 1874         6460 my $method = $self->next::can;
18              
19             try {
20 1874     1874   73751 $self->$method(@rest)
21             } catch {
22 1 50   1   1015 if (m/Can't find source for (.+?) at/) {
23             my @presentsources = map {
24 1 100       32 (distance($_, $1))[0] < 3 ? " * $_ <-- Possible Match\n" : " $_\n";
  11         11810  
25             } sort $self->storage->schema->sources;
26              
27 1         998 die <<"ERR";
28             $_
29             The ResultSet "$1" is not part of your schema.
30              
31             To help you debug this issue, here's a list of the actual sources that the
32             schema knows about:
33              
34             @presentsources
35             ERR
36             }
37 0           die $_;
38             }
39 1874         22117 }
40              
41             1;
42              
43             __END__
44              
45             =pod
46              
47             =head1 NAME
48              
49             DBIx::Class::Helper::Schema::DidYouMean - Nice error messages when you misspell the name of a ResultSet
50              
51             =head1 SYNOPSIS
52              
53             package MyApp::Schema;
54              
55             __PACKAGE__->load_components('Helper::Schema::DidYouMean');
56              
57             Elsewhere:
58              
59             $schema->resultset('Usre')->search(...)->...
60              
61             And a nice exception gets thrown:
62              
63             The ResultSet "Usre" is not part of your schema.
64            
65             To help you debug this issue, here's a list of the actual sources that the
66             schema knows about:
67            
68             Account
69             Permission
70             Role
71             * User <-- Possible Match
72              
73             =head1 DESCRIPTION
74              
75             This helper captures errors thrown when you use the C<resultset> method on your
76             schema and typo the source name. It tries to highlight the best guess as to
77             which you meant to type.
78              
79             =head1 AUTHOR
80              
81             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
82              
83             =head1 COPYRIGHT AND LICENSE
84              
85             This software is copyright (c) 2020 by Arthur Axel "fREW" Schmidt.
86              
87             This is free software; you can redistribute it and/or modify it under
88             the same terms as the Perl 5 programming language system itself.
89              
90             =cut