Crop/Error.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 85 CC 7 SCAR 31.9
CC
7
Cov
0%
warn
56.0
all_right
2.0
bind
2.0
CRAP 24.2
1 package Crop::Error;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Crop::Error - Error handling utilities for the Crop framework
8  
9 =head1 SYNOPSIS
10  
11 use Crop::Error;
12 # ...usage...
13  
14 =head1 DESCRIPTION
15  
16 Crop::Error provides error handling utilities for the Crop framework.
17  
18 =head1 AUTHORS
19  
20 Euvgenio (Core Developer)
21  
22 Alex (Contributor)
23  
24 =head1 COPYRIGHT AND LICENSE
25  
26 Apache 2.0
27  
28 =cut
29  
30 use base qw/ Exporter /;
31  
32 =begin nd
33 Class: Crop::Error
34 Basic error-handle functions.
35
36 This module redefines original perl function warn that has the 'true' return value, instead of
37 the return value of this function. Be aware.
38 =cut
39  
40 use v5.14;
41 use warnings;
42  
43 use Time::Stamp -stamps => {dt_sep => ' ', ms => 1};
44  
45 use Crop;
46  
47 =begin nd
48 Constants: Error levels.
49  
50 Semantics is comming from the kernel error messages.
51  
52 Constant: EMERG
53 The system is broken absolutly.
54  
55 The most critically strong level.
56 Unique sufficient choise for this state is to die silently and securitily is possible.
57  
58 Constant: ALERT
59 Fatal error of the main system.
60
61 System can not do their work correctly so it must be stopped gracefully
62 and terminate current request with respect to the user.
63  
64 Constant: CRIT
65 Critical resource error.
66
67 May be temporary error. This state require sufficient reaction.
68  
69 Tipical causes are: unreachable third-party service or resource is busy, missing config file, etc.
70  
71 Constant: ERR
72 Business logic error.
73
74 System is still in working state, but normal logic of request hadnler is broken. Reaction for
75 this situation is defined by upper level code.
76  
77 Constant: WARNING
78 Default level.
79
80 User input is wrong.
81  
82 Constant: NOTICE
83 Notable event that have not error semanitcs.
84  
85 For example, user has registered.
86  
87 Constant: INFO
88 User-defined event. It is not error.
89 =cut
90 use constant {
91 EMERG => 'EMERG', # 0
92 ALERT => 'ALERT', # 1
93 CRIT => 'CRIT', # 2
94 ERR => 'ERR', # 3
95 WARNING => 'WARNING', # 4
96 NOTICE => 'NOTICE', # 5
97 INFO => 'INFO', # 6
98 };
99  
100 =begin nd
101 Variable: our @EXPORT
102 By default is exported only basic <warn ( )> function.
103
104 Variable: our @EXPORT_OK
105 Exported by order <all_right ( )> and <has_error ( )>.
106 =cut
107 our @EXPORT = qw/ &warn /;
108 our @EXPORT_OK = qw/ &all_right &has_error /;
109  
110 =begin nd
111 Variable: my @Level
112 Logging levels in order of decreasing fatality (strong errors first).
113  
114 Variable: my %Level
115 Maps LogLevel to the index number.
116 =cut
117 my @Level = (
118 EMERG,
119 ALERT,
120 CRIT,
121 ERR,
122 WARNING,
123 NOTICE,
124 INFO,
125 );
126 my (%Level_ix, $i, $level);
127 $Level_ix{$level} = $i while ($i, $level) = each @Level;
128  
129 =begin nd
130 Variable: my @Stack
131 Stack of error Codes.
132 =cut
133 my @Stack;
134  
135 =begin nd
136 Function: all_right ( )
137 Is exist an error?
138
139 Returns:
140 1 - if no errors
141 0 - an error has occured
142 =cut
143 sub all_right { not @Stack }
144  
145 =begin nd
146 Function: bind ($node)
147 Bind all errors to specified $node.
148  
149 Errors are presented as a hashref where keys are error codes and values are 1.
150
151 Server loop uses this function to put in error codes to the output flow.
152  
153 > Crop::Error::bind $server->O->{ERROR};
154  
155 Params:
156 $node - where to put in the errors; not a reference, but direct place
157  
158 Returns:
159 Nothing, but result go to the $node.
160 =cut
161 sub bind($) { $_[0]->{$_} = 1 for @Stack }
162  
163 =begin nd
164 Method: erase ( )
165 Erase all the errors.
166
167 <all_right ( )> function will now return true.
168 =cut
169 sub erase { @Stack = () }
170  
171 =begin nd
172 Function: _parse ($msg)
173 Parse error message.
174
175 Result consists 3 parts: log Level, short code, instant message.
176  
177 'logLevel' directive in main config file determines either print a message to the log file.
178  
179 Parameters:
180 $msg - source text
181  
182 Returns:
183 An array of:
184
185 $log_level - print to log file?
186 $code - error code
187 $text - human readabe text
188 =cut
189 sub _parse {
190 my $msg = shift;
191  
192 my ($code, $log_level, $text) = $msg =~ /
193 ^\s* # skip spaces at the begining
194 (?:
195 (?:(\w*)\s*)? # Code
196 (?:\|\s*(\w*)\s*)? # Log Level
197 :)?
198 \s*(.*)$ # Text
199 /sx;
200  
201 $text ||= 'Undefined error';
202 $log_level ||= WARNING;
203  
204 my $level = 0;
205 my @cur_stack;
206  
207 # examine the stack of the current call
208 while ( my ($package, $file, $line) = caller $level++) {
209 push @cur_stack, " $package:$line ";
210 }
211 $text .= ' at' . join ' => ', reverse @cur_stack;
212  
213 ($log_level, $code, $text);
214 }
215  
216 =begin nd
217 Function: warn (@msg_parts)
218 Set error.
219
220 Remember the error code, print error message to the log file, and return undef.
221  
222 @message item format: 'CODE | LEVEL : Message'. Consists:
223
224 CODE - user defined string will be added to the output
225 LEVEL - restrictes print to log for slight errors
226 Message - text of an error to print as is
227  
228 LEVEL is one of:
229
230 - EMERG - the system is broken absolutely
231 - ALERT - fatal error of the main system
232 - CRIT - critical resource error
233 - ERR - business logic error
234 - WARNING - default level; user input is wrong
235 - NOTICE - notable event that have not error semanitcs
236 - INFO - user defined event
237
238 Separators only have meaning, not spaces.
239  
240 Note <Crop::Debug::debug()> is not related to LEVELs.
241
242 In contrast to standard warn() this <warn (messge) returns undef.
243
244 In order to redefine standard warn() function you have to 'use Crop::Error'. It is important, since can produce awkward situations.
245  
246 Parameters:
247 @msg_parts - entire message;
248 Message consists of 3 components: Error, Level, Text.
249  
250 Returns:
251 undef
252  
253 Example:
254 (start code)
255 # Full example: Error + Level + Text
256 warn 'NOUSER|ERR: User not defined';
257  
258 # Error + Text
259 warn 'SYSTEM:', $!;
260  
261 # Level + Text
262 warn '| NOTICE : New user has registered';
263  
264 # Text only
265 warn("Letter has received...");
266 (end)
267 =cut
268 sub warn {
269 my ($important, $code, $text) = _parse(join ' ', map defined ? $_ : 'undef', @_);
270  
271 push @Stack, $code if defined $code;
272
273 # print to log
274 if ($Level_ix{Crop->C->{logLevel}} >= $Level_ix{$important}) {
275 my $log_message;
276 $log_message .= "<$code> " if $code;
277 $log_message .= "$text" if $text;
278 $log_message .= "\n";
279  
280 my $script = $0 =~ /public_html(\S+)/ ? $1 : '';
281 print STDERR localstamp . " $script (pid=$$): $log_message";
282 flush STDERR;
283 }
284  
285 undef;
286 }
287  
288 1;