File Coverage

blib/lib/Perl/Critic/Policy/TryTiny/RequireCatch.pm
Criterion Covered Total %
statement 29 30 96.6
branch 6 8 75.0
condition n/a
subroutine 10 11 90.9
pod 4 5 80.0
total 49 54 90.7


line stmt bran cond sub pod time code
1 1     1   805 use strict;
  1         1  
  1         40  
2 1     1   4 use warnings;
  1         1  
  1         35  
3 1     1   4 use utf8;
  1         1  
  1         7  
4             package Perl::Critic::Policy::TryTiny::RequireCatch;
5             # ABSTRACT: Always include a "catch" block when using "try"
6             $Perl::Critic::Policy::TryTiny::RequireCatch::VERSION = '0.001';
7 1     1   40 use Readonly;
  1         1  
  1         57  
8 1     1   5 use Perl::Critic::Utils qw( :severities :classification :ppi );
  1         1  
  1         68  
9              
10 1     1   311 use base 'Perl::Critic::Policy';
  1         1  
  1         235  
11              
12             Readonly::Scalar my $DESC => "Try::Tiny \"try\" invoked without a corresponding \"catch\" block";
13             Readonly::Scalar my $EXPL => "Try::Tiny's \"try\" without a \"catch\" block will swallow exceptions. Use an empty block if this is intended.";
14              
15             sub supported_parameters {
16 4     4 0 12886 return ();
17             }
18              
19             sub default_severity {
20 2     2 1 23 return $SEVERITY_HIGH;
21             }
22              
23             sub default_themes {
24 0     0 1 0 return qw(bugs);
25             }
26              
27             sub applies_to {
28 4     4 1 30975 return 'PPI::Token::Word';
29             }
30              
31             sub violates {
32 20     20 1 426 my ($self, $elem, undef) = @_;
33              
34 20 100       38 return if $elem->content ne 'try';
35 4 50       27 return if ! is_function_call($elem);
36              
37 4 50       1012 my $try_block = $elem->snext_sibling() or return;
38 4         60 my $sib = $try_block->snext_sibling();
39 4 100       49 if ($sib->content ne 'catch') {
40 2         19 return $self->violation($DESC, $EXPL, $elem);
41             }
42 2         13 return;
43             }
44              
45             1;
46              
47             __END__
48              
49             =pod
50              
51             =encoding UTF-8
52              
53             =head1 NAME
54              
55             Perl::Critic::Policy::TryTiny::RequireCatch - Always include a "catch" block when using "try"
56              
57             =head1 VERSION
58              
59             version 0.001
60              
61             =head1 DESCRIPTION
62              
63             Programmers from other programming languages may assume that the following code
64             does not swallow exceptions:
65              
66             use Try::Tiny;
67             try {
68             # ...
69             }
70             finally {
71             # ...
72             };
73              
74             However, Try::Tiny's implementation always swallows exceptions unless a catch
75             block has been specified.
76              
77             The programmer should always include a catch block. The block may be empty, to
78             indicate that exceptions are deliberately ignored.
79              
80             use Try::Tiny;
81             try {
82             # ...
83             }
84             catch {
85             # ...
86             }
87             finally {
88             # ...
89             };
90              
91             =head1 CONFIGURATION
92              
93             This Policy is not configurable except for the standard options.
94              
95             =head1 KNOWN BUGS
96              
97             This policy assumes that L<Try::Tiny> is being used, and doesn't check for
98             whether an alternative like L<TryCatch>.
99              
100             =head1 AUTHOR
101              
102             David D Lowe <flimm@cpan.org>
103              
104             =head1 COPYRIGHT AND LICENSE
105              
106             This software is copyright (c) 2014 by Lokku <cpan@lokku.com>.
107              
108             This is free software; you can redistribute it and/or modify it under
109             the same terms as the Perl 5 programming language system itself.
110              
111             =cut