blib/lib/App/karr/Error.pm

filter: Statementstmt 54.8% 34 / 62 Branchbran 18.7% 3 / 16 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 37.5% 6 / 16 Podpod 100% 9 / 9 totaltotal 44.9% 53 / 118 CC 12 SCAR 17.4
CC
12
Cov
44%
is_usage_error
12.0
_karr_claim_unset_clause
12.0
clean_error
6.2
CRAP 5.7
« blib/lib/App/karr/Role/Output.pm ↑ Summary blib/lib/App/karr/Foundation/Picker.pm »
1 # ABSTRACT: Turn internal errors into one clean user-facing line
2  
3 package App::karr::Error;
4 37 our $VERSION = '0.602';
5 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 37 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 37 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 37 our @EXPORT_OK = qw( user_error clean_error is_usage_error command_hint
11 original_argv set_original_argv require_claim_message mandatory_claim_message
12 held_back_claim_message );
13  
14  
15 # The indentation every suggestion line carries, and the pattern that finds one
16 # again at the end of a message. Two spaces and the program name, so the line is
17 # recognisable without a marker of its own -- which matters because clean_error
18 # below has to tell karr's own answer apart from the backend chatter it exists
19 # to cut away.
20 37 my $HINT_INDENT = ' ';
21 # A suggestion line is ` karr ...` -- and, since ADR 0005, the ` export
22 # KARR_CLAIM=...` line that names the second way to claim (require_claim_message
23 # below). Both are part of the answer clean_error must carry past the first-line
24 # reduction, so a `move` into a require_claim column keeps both ways out even
25 # when its per-id failure is cleaned by the batch runner.
26 37 my $HINT_LINE = qr{\n\Q$HINT_INDENT\E(?:karr |export KARR_CLAIM)[^\n]*};
27  
28 0 ▶sub command_hint {
Subroutine command_hint: not called CC=2 SCAR=17.9
Pod documented
29 0 my (@tokens) = @_;
30 return $HINT_INDENT
31 0 ▶ . join ' ', 'karr', map { _hint_token($_) } grep { defined && length } @tokens;
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
32 }
33  
34  
35 # The argv as the caller typed it, kept for the suggestion lines. F<bin/karr>
36 # records it once and does it BEFORE the two rewrites that follow, because
37 # neither leaves the caller's own words standing:
38 # App::karr::Role::CliArgs/normalize_option_argv respells --claimed-by as
39 # --claimed_by (#256) and folds a flag-shaped value onto its option with an `=`
40 # (#259). A suggestion has to show what was typed, so it is read from here and
41 # not from @ARGV.
42 #
43 # Nothing else records one. A library caller, a test that loads a command class
44 # directly and F<bin/karr-foundation> all read back undef, and the suggestion
45 # line is then left out rather than guessed at (ticket k263).
46 37 my $ORIGINAL_ARGV;
47  
48 0 ▶sub set_original_argv {
Subroutine set_original_argv: not called CC=1 SCAR=6.9
Pod documented
49 0 my (@argv) = @_;
50 0 $ORIGINAL_ARGV = [@argv];
51 0 return;
52 }
53  
54  
55 0 ▶sub original_argv {
Subroutine original_argv: not called CC=2 SCAR=17.9
Pod documented
56 0 ▶ return unless defined $ORIGINAL_ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ORIGINAL_ARGV 0 0 0
57 0 return [@$ORIGINAL_ARGV];
58 }
59  
60  
61 0 ▶sub _hint_token {
Subroutine _hint_token: not called CC=2 SCAR=17.9
62 0 my ($token) = @_;
63 0 ▶ return $token if $token =~ m{\A[\w.,:=/@+#%-]+\z};
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ m[\A[\w.,:=/\@+#%-]+\z] 0 0 0
64 # POSIX single quoting: the only character a single-quoted string cannot hold
65 # is the quote itself, which closes, escapes and reopens.
66 0 $token =~ s/'/'\\''/g;
67 0 return "'" . $token . "'";
68 }
69  
70 2 ▶sub clean_error {
Subroutine clean_error: called CC=5 SCAR=18.2
Pod documented
71 2 my ($err) = @_;
72  
73 # libgit2 exceptions (Git::Libgit2::Error) put the useful text in ->message;
74 # everything else in karr's path (Path::Tiny::Error, plain die strings)
75 # stringifies usefully.
76 2 ▶ my $detail = blessed($err) && $err->can('message') ? $err->message : "$err";
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($err) && $err->can('message') ? : 0 2 2
Condition33%1/3
builtin::blessed($err) and $err->can("message")
!ll&&!rl&&r
020
MC/DC0%0/2
builtin::blessed($err) and $err->can("message")
builtin::blessed($err) $err->can("message")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->can("message")
ABresult
0X0
100
111
77 2 $detail =~ s/\s+\z//;
78  
79 # karr's own suggestion block comes off the end first and goes back on last.
80 # It is the one part of a message that must survive the first-line reduction
81 # below -- App::karr::Role::TaskMutation's batch runner cleans every per-id
82 # failure through here before printing it, and a `move` that answered
83 # "Status 'in-progress' requires a claim:" with the answer itself cut off
84 # would be worse than the message this replaced (ticket k263).
85 2 my @hint;
86 2 ▶ while ( $detail =~ s/($HINT_LINE)\z// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($detail =~ s/($HINT_LINE)\z//) 0 2 2
87 0 my $line = $1;
88 0 $line =~ s/\A\n//;
89 0 unshift @hint, $line;
90 }
91  
92 2 $detail =~ s/ at \S+ line \d+\.?.*\z//s; # the call site and all that follows
93 2 $detail =~ s/\n.*\z//s; # keep only the first line
94 2 $detail =~ s/\s+\z//;
95 2 ▶ return 'unknown error' unless length $detail;
Branch: 1 branch
BranchTrueFalseTotal
unless length $detail 0 2 2
96 2 return join "\n", $detail, @hint;
97 }
98  
99  
100 1 ▶sub user_error {
Subroutine user_error: called CC=1 SCAR=0.0
Pod documented
101 1 my (@parts) = @_;
102 1 my $msg = join '', grep { defined } @parts;
103 1 $msg =~ s/\s+\z//;
104 # A plain die, not croak: die honours the trailing newline and appends no
105 # call site. bin/karr's central handler prints this verbatim and maps it to
106 # the exit code (ADR 0002), so messages that should exit 2 rather than 1 have
107 # to start with one of its usage markers ("Usage:" is the one for a bad
108 # option value).
109 1 die "$msg\n";
110 }
111  
112  
113 # The stable leading markers a usage-error die carries (ADR 0002). This used to
114 # live only in the regex in bin/karr's central handler, which was fine while
115 # that handler was the only reader. App::karr::Role::TaskMutation's batch runner
116 # is the second: it has to tell a failure that belongs to one id apart from one
117 # that condemns the whole invocation, and a second copy of the marker list would
118 # drift the moment either side gained a marker -- the batch would then quietly
119 # demote a usage error (2) to a runtime failure (1).
120 37 my $USAGE_MARKERS = qr{
121 \A (?: Unknown\ command: # the dispatch guard in App::karr
122 | unexpected\ extra\ argument # surplus positionals (Role::CliArgs)
123 | Usage: # a missing required positional
124 | Usage\ error: # anything usage_error() raises
125 )
126 }x;
127  
128 0 ▶sub is_usage_error {
Subroutine is_usage_error: not called CC=3 SCAR=24.8
Pod documented
129 0 my ($err) = @_;
130 0 ▶ return 0 unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 0 0 0
131 0 ▶ return "$err" =~ $USAGE_MARKERS ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
"$err" =~ /$USAGE_MARKERS/ ? : 0 0 0
132 }
133  
134  
135 # The two ways out of a missing claim, side by side (ADR 0005): the invocation
136 # with --claim added, and the once-per-session export. The `karr` line comes
137 # LAST so a `tail -n` keeps the immediate copy-paste fix (the k263 rule), with
138 # the session-level export advice above it; both are shaped so $HINT_LINE
139 # rescues them through clean_error.
140 0 ▶sub _claim_ways_out {
Subroutine _claim_ways_out: not called CC=1 SCAR=6.9
141 0 my (@hint_tokens) = @_;
142 0 return $HINT_INDENT
143 . 'export KARR_CLAIM=$(karr agent-name) # once per session' . "\n"
144 . command_hint(@hint_tokens);
145 }
146  
147 # The "and KARR_CLAIM is unset" clause, only when it truly is (ADR 0005). A
148 # presence test on %ENV, not a value read, so nothing crosses the encoding
149 # boundary here.
150 0 ▶sub _karr_claim_unset_clause {
Subroutine _karr_claim_unset_clause: not called CC=3 SCAR=24.8
151 return ( defined $ENV{KARR_CLAIM} && length $ENV{KARR_CLAIM} )
152 0 ▶ ? ''
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'KARR_CLAIM'} && length $ENV{'KARR_CLAIM'} ? : 0 0 0
Condition0%0/3
defined $ENV{"KARR_CLAIM"} and length $ENV{"KARR_CLAIM"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{"KARR_CLAIM"} and length $ENV{"KARR_CLAIM"}
defined $ENV{"KARR_CLAIM"} length $ENV{"KARR_CLAIM"}
Truth table0%0/3
A and B
A= defined $ENV{"KARR_CLAIM"}B= length $ENV{"KARR_CLAIM"}
ABresult
0X0
100
111
153 : ', and KARR_CLAIM is unset';
154 }
155  
156 0 ▶sub require_claim_message {
Subroutine require_claim_message: not called CC=1 SCAR=6.9
Pod documented
157 0 my ( $status, @hint_tokens ) = @_;
158 0 return "Status '$status' requires a claim"
159 . _karr_claim_unset_clause() . ":\n"
160 . _claim_ways_out(@hint_tokens);
161 }
162  
163  
164 0 ▶sub mandatory_claim_message {
Subroutine mandatory_claim_message: not called CC=1 SCAR=6.9
Pod documented
165 0 my ( $command, @hint_tokens ) = @_;
166 0 return "karr $command needs a claim"
167 . _karr_claim_unset_clause() . ":\n"
168 . _claim_ways_out(@hint_tokens);
169 }
170  
171  
172 # The other side of require_claim_message (ticket k306): a column that refuses
173 # a claim rather than one that demands it. No KARR_CLAIM line here -- the env
174 # default is never written into backlog (App::karr::Role::ClaimDefault), so
175 # only an explicit --claim reaches this, and the way out is the one command
176 # that takes the card out of the column.
177 0 ▶sub held_back_claim_message {
Subroutine held_back_claim_message: not called CC=1 SCAR=6.9
Pod documented
178 0 my ( $status, @hint_tokens ) = @_;
179 0 return "Status '$status' holds no claim -- promote the card first and claim it there:\n"
180 . command_hint(@hint_tokens);
181 }
182  
183  
184 37 1;
185  
186 __END__
« blib/lib/App/karr/Role/Output.pm ↑ Summary blib/lib/App/karr/Foundation/Picker.pm »