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.035000';
3             # ABSTRACT: Nice error messages when you misspell the name of a ResultSet
4              
5 56     56   28066 use strict;
  56         151  
  56         1669  
6 56     56   291 use warnings;
  56         122  
  56         1602  
7              
8 56     56   301 use parent 'DBIx::Class::Schema';
  56         129  
  56         301  
9              
10 56     56   30045 use Text::Brew 'distance';
  56         83638  
  56         4135  
11 56     56   472 use Try::Tiny;
  56         129  
  56         2831  
12 56     56   395 use namespace::clean;
  56         155  
  56         399  
13              
14             sub source {
15 1873     1873 1 9124902 my ($self, @rest) = @_;
16              
17 1873         6946 my $method = $self->next::can;
18              
19             try {
20 1873     1873   82120 $self->$method(@rest)
21             } catch {
22 1 50   1   1186 if (m/Can't find source for (.+?) at/) {
23             my @presentsources = map {
24 1 100       36 (distance($_, $1))[0] < 3 ? " * $_ <-- Possible Match\n" : " $_\n";
  11         14287  
25             } sort $self->storage->schema->sources;
26              
27 1         1243 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 1873         24388 }
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