The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Log::Report::Dispatcher::Try - capture all reports as exceptions

INHERITANCE

 Log::Report::Dispatcher::Try
   is a Log::Report::Dispatcher

SYNOPSIS

 try { ... };       # mind the ';' !!
 if($@) {           # signals something went wrong

 if(try {...}) {    # block ended normally

 my $x = try {read_temperature()};
 my @x = try {read_lines_from_file()};

 try { ... }        # no comma!!
    mode => 'DEBUG', accept => 'ERROR-';

 try sub { ... },   # with comma
    mode => 'DEBUG', accept => 'ALL';

 try \&myhandler, accept => 'ERROR-';

 print ref $@;      # Log::Report::Dispatcher::Try

 $@->reportFatal;   # re-dispatch result of try block
 $@->reportAll;     # ... also warnings etc
 if($@) {...}       # if errors
 if($@->failed) {   # same       # }
 if($@->success) {  # no errors  # }

 try { report {to => 'stderr'}, FAILURE => 'no network' };
 $@->reportFatal(to => 'syslog');  # overrule destination

DESCRIPTION

The Log::Report::try() catches errors in the block (CODE reference) which is just following the function name. All dispatchers are temporarily disabled by try, and messages which are reported are collected within a temporary dispatcher named try. When the CODE has run, that try dispatcher is returned in $@, and all original dispatchers reinstated.

Then, after the try has finished, the routine which used the "try" should decide what to do with the collected reports. These reports are collected as Log::Report::Exception objects. They can be ignored, or thrown to a higher level try... causing an exit of the program if there is none.

METHODS

Constructors

$obj->close

    Only when initiated with a FILENAME, the file will be closed. In any other case, nothing will be done.

Log::Report::Dispatcher::Try->new(TYPE, NAME, OPTIONS)

     Option       --Defined in     --Default
     accept         Log::Report::Dispatcher  depend on mode
     died                            undef
     exceptions                      []
     format_reason  Log::Report::Dispatcher  'LOWERCASE'
     locale         Log::Report::Dispatcher  <system locale>
     mode           Log::Report::Dispatcher  'NORMAL'

    . accept => REASONS

    . died => STRING

      The exit string ($@) of the eval'ed block.

    . exceptions => ARRAY-of-EXCEPTIONS

    . format_reason => 'UPPERCASE'|'LOWERCASE'|'UCFIRST'|'IGNORE'|CODE

    . locale => LOCALE

    . mode => 'NORMAL'|'VERBOSE'|'ASSERT'|'DEBUG'|0..3

Accessors

$obj->died([STRING])

    The message which was reported by eval, which is used internally to catch problems in the try block.

$obj->exceptions

    Returns all collected Log::Report::Exceptions. The last of them may be a fatal one. The other are non-fatal.

$obj->isDisabled

$obj->mode

$obj->name

$obj->needs

$obj->type

Logging

$obj->collectLocation

Log::Report::Dispatcher::Try->collectLocation

$obj->collectStack([MAXDEPTH])

$obj->log(OPTS, REASON, MESSAGE)

    Other dispatchers translate the message here, and make it leave the program. However, messages in a "try" block are only captured in an intermediate layer: they may never be presented to an end-users. And for sure, we do not know the language yet.

    The MESSAGE is either a STRING or a Log::Report::Message.

$obj->reportAll(OPTIONS)

$obj->reportFatal

$obj->stackTraceLine(OPTIONS)

Log::Report::Dispatcher::Try->stackTraceLine(OPTIONS)

$obj->translate(HASH-of-OPTIONS, REASON, MESSAGE)

Status

$obj->failed

    Returns true if the block was left with an fatal message.

$obj->showStatus

    If this object is kept in $@, and someone uses this as string, we want to show the fatal error message.

    The message is not very informative for the good cause: we do not want people to simply print the $@, but wish for a re-cast of the message using reportAll() or reportFatal().

$obj->success

    Returns true if the block exited normally.

$obj->wasFatal(OPTIONS)

    Returns the Log::Report::Exception which caused the "try" block to die, otherwise an empty LIST (undef).

     Option--Default
     class   undef

    . class => CLASS|REGEX

      Only return the exception if it was fatal, and in the same time in the specified CLASS (as string) or matches the REGEX. See Log::Report::Message::inClass()

DETAILS

SEE ALSO

This module is part of Log-Report distribution version 0.15, built on February 25, 2008. Website: http://perl.overmeer.net/log-report/

LICENSE

Copyrights 2007-2008 by Mark Overmeer. For other contributors see ChangeLog.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html