File Coverage

blib/lib/Perinci/Sub/Property/exit_on_error.pm
Criterion Covered Total %
statement 29 30 96.6
branch 7 10 70.0
condition 5 11 45.4
subroutine 5 5 100.0
pod n/a
total 46 56 82.1


line stmt bran cond sub pod time code
1             package Perinci::Sub::Property::exit_on_error;
2              
3 1     1   23056 use 5.010001;
  1         4  
  1         43  
4 1     1   8 use strict;
  1         2  
  1         38  
5 1     1   6 use warnings;
  1         2  
  1         109  
6             #use Log::Any '$log';
7              
8 1     1   1033 use Perinci::Sub::PropertyUtil qw(declare_property);
  1         931  
  1         411  
9              
10             our $VERSION = '0.01'; # VERSION
11              
12             declare_property(
13             name => 'exit_on_error',
14             type => 'function',
15             schema => ['any*' => {of=>[
16             ['bool' => {default=>0}],
17             ['hash*' => {keys=>{
18             'success_statuses' => ['regex' => {default=>'^(2..|304)$'}],
19             }}],
20             ]}],
21             wrapper => {
22             meta => {
23             v => 2,
24             prio => 99, # very low, the last to do stuff to $_w_res
25             convert => 1,
26             },
27             handler => sub {
28 6     6   42916 my ($self, %args) = @_;
29 6   33     37 my $v = $args{new} // $args{value} // 0;
      50        
30 6         9 my $meta = $args{meta};
31              
32 6 50       20 return unless $v;
33              
34 6 100       64 die "Cannot use exit_on_error if result_naked is 1"
35             if $self->{_meta}{result_naked};
36              
37 4 100       14 $v = {} if ref($v) ne 'HASH';
38              
39 4   66     22 $v->{success_statuses} //= qr/^(2..|304)$/;
40              
41 4         6 for my $k (qw/success_statuses/) {
42 4 50 33     33 if (defined($v->{$k}) && ref($v->{$k}) ne 'Regexp') {
43 0         0 $v->{$k} = qr/$v->{$k}/;
44             }
45             }
46              
47 4         14 $self->select_section('after_eval');
48              
49 4         44 $self->push_lines('if ($_w_eval_err) { die $_w_eval_err }');
50              
51 4         114 $self->push_lines('if ($_w_res->[0] !~ /'.$v->{success_statuses}.'/) {');
52 4         54 $self->indent;
53 4 50       48 $self->push_lines(join(
54             "",
55             "warn 'Call to ",
56             ($self->{_args}{sub_name} ? "$self->{_args}{sub_name}()" : "function"),
57             q[ returned non-success status '. "$_w_res->[0]: $_w_res->[1]";]));
58 4         58 $self->push_lines('exit($_w_res->[0]-300);');
59 4         57 $self->unindent;
60 4         31 $self->push_lines('}');
61             },
62             },
63             );
64              
65             1;
66             # ABSTRACT: Die on non-success result
67              
68             __END__
69              
70             =pod
71              
72             =encoding UTF-8
73              
74             =head1 NAME
75              
76             Perinci::Sub::Property::exit_on_error - Die on non-success result
77              
78             =head1 VERSION
79              
80             version 0.01
81              
82             =head1 SYNOPSIS
83              
84             Without exit_on_error:
85              
86             # on successful call
87             f(...); # [200, "OK"]
88              
89             # on non-successful call
90             f(...); # [404, "Not found"]
91              
92             With C<< exit_on_error => 1 >>:
93              
94             # on successful call
95             f(...); # [200, "OK"]
96              
97             # on non-successful call
98             f(...); # print message "Call f() failed with 404 status: Not found" to STDERR and exits 104
99              
100             To customize what statuses are considered error: C<< exit_on_error => {
101             success_statuses => '^2..$' } >>.
102              
103             =head1 DESCRIPTION
104              
105             This property sets so that function calls C<exit()> when result status is a
106             non-successful one. Successful statuses by default include 2xx and 304 (C<<
107             '^(2..|304)$' >>).
108              
109             Exit code is currently set to (like in L<Perinci::CmdLine>) and might be
110             customizable in the future.
111              
112             =head1 SEE ALSO
113              
114             L<Perinci>
115              
116             =head1 HOMEPAGE
117              
118             Please visit the project's homepage at L<https://metacpan.org/release/Perinci-Sub-Property-exit_on_error>.
119              
120             =head1 SOURCE
121              
122             Source repository is at L<https://github.com/sharyanto/perl-Perinci-Sub-Property-exit_on_error>.
123              
124             =head1 BUGS
125              
126             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Sub-Property-exit_on_error>
127              
128             When submitting a bug or request, please include a test-file or a
129             patch to an existing test-file that illustrates the bug or desired
130             feature.
131              
132             =head1 AUTHOR
133              
134             Steven Haryanto <stevenharyanto@gmail.com>
135              
136             =head1 COPYRIGHT AND LICENSE
137              
138             This software is copyright (c) 2014 by Steven Haryanto.
139              
140             This is free software; you can redistribute it and/or modify it under
141             the same terms as the Perl 5 programming language system itself.
142              
143             =cut