File Coverage

blib/lib/Perinci/Sub/Property/exit_on_error.pm
Criterion Covered Total %
statement 28 29 96.5
branch 7 10 70.0
condition 4 11 36.3
subroutine 5 5 100.0
pod n/a
total 44 55 80.0


line stmt bran cond sub pod time code
1             package Perinci::Sub::Property::exit_on_error;
2              
3 1     1   26388 use 5.010001;
  1         4  
4 1     1   6 use strict;
  1         2  
  1         26  
5 1     1   9 use warnings;
  1         4  
  1         81  
6             #use Log::Any '$log';
7              
8 1     1   516 use Perinci::Sub::PropertyUtil qw(declare_property);
  1         772  
  1         350  
9              
10             our $VERSION = '0.03'; # 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   42965 my ($self, %args) = @_;
29 6   33     28 my $v = $args{new} // $args{value} // 0;
      0        
30 6         13 my $meta = $args{meta};
31              
32 6 50       19 return unless $v;
33              
34             die "Cannot use exit_on_error if result_naked is 1"
35 6 100       55 if $self->{_meta}{result_naked};
36              
37 4 100       17 $v = {} if ref($v) ne 'HASH';
38              
39 4   66     28 $v->{success_statuses} //= qr/^(2..|304)$/;
40              
41 4         11 for my $k (qw/success_statuses/) {
42 4 50 33     29 if (defined($v->{$k}) && ref($v->{$k}) ne 'Regexp') {
43 0         0 $v->{$k} = qr/$v->{$k}/;
44             }
45             }
46              
47 4         15 $self->select_section('after_eval');
48              
49 4         59 $self->push_lines('if ($_w_eval_err) { die $_w_eval_err }');
50              
51 4         119 $self->push_lines('if ($_w_res->[0] !~ /'.$v->{success_statuses}.'/) {');
52 4         60 $self->indent;
53             $self->push_lines(join(
54             "",
55             "warn 'Call to ",
56 4 50       51 ($self->{_args}{sub_name} ? "$self->{_args}{sub_name}()" : "function"),
57             q[ returned non-success status '. "$_w_res->[0]: $_w_res->[1]";]));
58 4         61 $self->push_lines('exit($_w_res->[0]-300);');
59 4         63 $self->unindent;
60 4         64 $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             This document describes version 0.03 of Perinci::Sub::Property::exit_on_error (from Perl distribution Perinci-Sub-Property-exit_on_error), released on 2016-05-11.
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 HOMEPAGE
113              
114             Please visit the project's homepage at L<https://metacpan.org/release/Perinci-Sub-Property-exit_on_error>.
115              
116             =head1 SOURCE
117              
118             Source repository is at L<https://github.com/perlancar/perl-Perinci-Sub-Property-exit_on_error>.
119              
120             =head1 BUGS
121              
122             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>
123              
124             When submitting a bug or request, please include a test-file or a
125             patch to an existing test-file that illustrates the bug or desired
126             feature.
127              
128             =head1 SEE ALSO
129              
130             L<Perinci>
131              
132             =head1 AUTHOR
133              
134             perlancar <perlancar@cpan.org>
135              
136             =head1 COPYRIGHT AND LICENSE
137              
138             This software is copyright (c) 2016 by perlancar@cpan.org.
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