The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Error::Pure::Utils - Utilities for structured errors.

SYNOPSIS

 use Error::Pure::Utils qw(clean err_get err_helper err_msg err_msg_hr);

 clean();
 my @errors = err_get($clean);
 my @err_msg = err_msg($index);
 my $err_msg_hr = err_msg_hr($index);
 my @errors = err_helper('This is a fatal error', 'name', 'value');

SUBROUTINES

clean

 clean();

Resets internal variables with errors. It is exportable.

Returns undef.

err_get

 my @errors = err_get($clean);

Get and clean processed errors. err_get() returns error structure. err_get(1) returns error structure and delete it internally. It is exportable.

Returns array of errors.

err_msg

 my @err_msg = err_msg($index);

Get $index error messages array. If $index isn't present, use -1 as last message. Is is usable in situation >>err 'Error', 'item1', 'item2', 'item3', 'item4'<<. Then returns ('Error', 'item1', 'item2', 'item3', 'item4') array. See EXAMPLE2. It is exportable.

Returns array of error messages.

err_msg_hr

 my $err_msg_hr = err_msg_hr($index);

Get $index error message key, value pairs as hash reference. If $index isn't present, use -1 as last message. Is is usable in situation >>err 'Error', 'key1', 'val1', 'key2', 'val2'<<. Then returns {'key1' => 'val1', 'key2' => 'val2'} structure. See EXAMPLE3. It is exportable.

Returns reference to hash with error messages.

err_helper

 my @errors = err_helper('This is a fatal error', 'name', 'value');

Subroutine for additional module above Error::Pure. @msg is array of messages. If last error is undef, rewrite it to 'undef' string. If @msg is blank, add 'undef' string. Chomp last error. It is exportable.

Returns array of errors.

VARIABLES

$LEVEL

Default value is 2.

$MAX_LEVELS

Default value is 50.

$MAX_EVAL

Default value is 100.

$MAX_ARGS

Default value is 10.

$MAX_ARG_LEN

Default value is 50.

$PROGRAM
 Program name in stack information.
 Default value is ''.

EXAMPLE1

 use strict;
 use warnings;

 use Dumpvalue;
 use Error::Pure::Die qw(err);
 use Error::Pure::Utils qw(err_get);

 # Error in eval.
 eval { err '1', '2', '3'; };

 # Error structure.
 my @err = err_get();

 # Dump.
 my $dump = Dumpvalue->new;
 $dump->dumpValues(\@err);

 # In \@err:
 # [
 #         {
 #                 'msg' => [
 #                         '1',
 #                         '2',
 #                         '3',
 #                 ],
 #                 'stack' => [
 #                         {
 #                                 'args' => '(1)',
 #                                 'class' => 'main',
 #                                 'line' => '9',
 #                                 'prog' => 'script.pl',
 #                                 'sub' => 'err',
 #                         },
 #                         {
 #                                 'args' => '',
 #                                 'class' => 'main',
 #                                 'line' => '9',
 #                                 'prog' => 'script.pl',
 #                                 'sub' => 'eval {...}',
 #                         },
 #                 ],
 #         },
 # ],

EXAMPLE2

 use strict;
 use warnings;

 use English qw(-no_match_vars);
 use Error::Pure qw(err);
 use Error::Pure::Utils qw(err_msg);

 # Error in eval.
 eval {
         err 'Error', 'item1', 'item2', 'item3', 'item4';
 };
 if ($EVAL_ERROR) {
         my @err_msg = err_msg();
         foreach my $item (@err_msg) {
                 print "$item\n";
         }
 }

 # Output:
 # Error
 # item1
 # item2
 # item3
 # item4

EXAMPLE3

 use strict;
 use warnings;

 use English qw(-no_match_vars);
 use Error::Pure qw(err);
 use Error::Pure::Utils qw(err_msg_hr);

 # Error in eval.
 eval {
         err 'Error',
                 'key1', 'val1',
                 'key2', 'val2';
 };
 if ($EVAL_ERROR) {
         print $EVAL_ERROR;
         my $err_msg_hr = err_msg_hr();
         foreach my $key (sort keys %{$err_msg_hr}) {
                 print "$key: $err_msg_hr->{$key}\n";
         }
 }

 # Output:
 # Error
 # key1: val1
 # key2: val2

DEPENDENCIES

Cwd, Exporter, Readonly.

SEE ALSO

Task::Error::Pure

Install the Error::Pure modules.

REPOSITORY

https://github.com/michal-josef-spacek/Error-Pure

AUTHOR

Michal Josef Špaček mailto:skim@cpan.org

http://skim.cz

LICENSE AND COPYRIGHT

© 2008-2024 Michal Josef Špaček

BSD 2-Clause License

VERSION

0.33