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.034002';
3             # ABSTRACT: Nice error messages when you misspell the name of a ResultSet
4              
5 55     55   21534 use strict;
  55         115  
  55         1330  
6 55     55   239 use warnings;
  55         103  
  55         1179  
7              
8 55     55   229 use parent 'DBIx::Class::Schema';
  55         99  
  55         230  
9              
10 55     55   22883 use Text::Brew 'distance';
  55         65471  
  55         3222  
11 55     55   369 use Try::Tiny;
  55         105  
  55         2243  
12 55     55   309 use namespace::clean;
  55         97  
  55         384  
13              
14             sub source {
15 1846     1846 1 7275627 my ($self, @rest) = @_;
16              
17 1846         5799 my $method = $self->next::can;
18              
19             try {
20 1846     1846   67756 $self->$method(@rest)
21             } catch {
22 1 50   1   957 if (m/Can't find source for (.+?) at/) {
23             my @presentsources = map {
24 1 100       31 (distance($_, $1))[0] < 3 ? " * $_ <-- Possible Match\n" : " $_\n";
  11         11628  
25             } sort $self->storage->schema->sources;
26              
27 1         1057 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 1846         19742 }
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) 2019 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