NAME

Dist::Zilla::Role::ErrorLogger - Provide error logging capabilities for Dist::Zilla plugins

VERSION

Version 0.001.

SYNOPSIS

package Dist::Zilla::Plugin::YourPlugin;
use Moose;
use namespace::autoclean;
with 'Dist::Zilla::Role::Plugin';
with 'Dist::Zilla::Role::ErrorLogger';

sub method {
    my $self = shift( @_ );
    …
    if ( … ) { $self->log_error( 'error message' ); };
    assertion_condition or $self->log_error( 'another error message' );
    …
    foreach … {
        …
        check or $self->log_error( 'error message' ) and next;
        …
    };
    …
    $self->abort_if_error();
};

__PACKAGE__->meta->make_immutable;
1;

DESCRIPTION

Dist::Zilla limits logging capabilities with 3 logging levels available in plugings through log_debug, log, and log_fatal methods. Debug level messages are turned off by default, the first fatal message terminates Dist::Zilla. This is simple, but sometimes you may want to report all the errors, instead of stopping at the first found one. In such a case log_fatal cannot be used, obviously. There are few alternatives:

Collect error messages in an array, then report all the errors with single log_fatal call:

my @errors;
…
push( @errors, … );
…
if ( @errors ) {
    $self->log_fatal( join( "\n", @errors ) );
};

This works, but current implementation of log_fatal has a disadvantage: it prints the message twice, so output will looks ugly. (See message handling in log_fatal is suboptimal.)

Another approach is reporting each error immediately with log, counting number of reported errors, and calling log_fatal once at the end:

my $error_count = 0;
…
$self->log( 'error' );
++ $error_count;
…
if ( $error_count ) {
    $self->log_fatal( 'Aborting...' );
};

This works, but incrementing the counter is boring and error-prone. <Dist-Zilla-Role-ErrorLogger> role automates it, making plugin code shorter and more readable:

with 'Dist::Zilla::Role::ErrorLogger';
…
$self->log_error( 'error' );
…
$self->abort_if_error();

ATTRIBUTES

error_count

Number of logged errors (i. e. number of log_error calls). Read-only.

METHODS

log_error

This method calls log method, passing all the arguments, and increments value of error_count attribute. The method returns true value, so can be used in following constructs:

while ( … ) {
    …
    assert_condition or $self->log_error( 'message' ) and next;
    …
};

abort_if_error

If there was any errors (i. e. error_count is greater than zero), the method aborts execution by calling log_fatal.

AUTHOR

Van de Bugger <van.de.bugger@gmail.com>

COPYRIGHT AND LICENSE

Copyright © 2015 Van de Bugger

This file is part of perl-Dist-Zilla-Role-ErrorLogger.

perl-Dist-Zilla-Role-ErrorLogger is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

perl-Dist-Zilla-Role-ErrorLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with perl-Dist-Zilla-Role-ErrorLogger. If not, see <http://www.gnu.org/licenses/>.