App-Sqitch

 view release on metacpan or  search on metacpan

lib/App/Sqitch/X.pm  view on Meta::CPAN

package App::Sqitch::X;

use 5.010;
use utf8;
use Moo;
use Types::Standard qw(Str Int);
use Sub::Exporter::Util ();
use Throwable 0.200009;
use Sub::Exporter -setup => [qw(hurl)];
use overload '""' => 'as_string';

our $VERSION = 'v1.6.1'; # VERSION

has message => (
    is       => 'ro',
    isa      => Str,
    required => 1,
);

has exitval => (
    is      => 'ro',
    isa     => Int,
    default => 2,
);

with qw(
    Throwable
    StackTrace::Auto
);

has ident => (
  is      => 'ro',
  isa     => Str,
  default => 'DEV'
);

has '+previous_exception' => (init_arg => 'previous_exception')
    if Throwable->VERSION < 0.200007;

sub hurl {
    @_ = (
        __PACKAGE__,
        # Always pass $@, as Throwable is unreliable about getting it thanks
        # to hash randomization. Its workaround in v0.200006:
        # https://github.com/rjbs/throwable/commit/596dfbafed970a30324dc21539d4edf2cbda767a
        previous_exception => $@,
        ref $_[0]     ? %{ $_[0] }
            : @_ == 1 ? (message => $_[0])
            :           (ident => $_[0],  message => $_[1])
    );
    goto __PACKAGE__->can('throw');
}

sub as_string {
    my $self = shift;
    join "\n", grep { $_ } (
        $self->message,
        $self->previous_exception,
        $self->stack_trace
    );
}

sub details_string {
    my $self = shift;
    join "\n", grep { $_ } (
        $self->previous_exception,
        $self->stack_trace
    );
}

1;

__END__

=head1 Name

App::Sqitch::X - Sqitch Exception class

=head1 Synopsis

  use Locale::TextDomain;
  use App::Sqitch::X qw(hurl);
  open my $fh, '>', 'foo.txt' or hurl {
      ident   => 'io',
      message => __x 'Cannot open {file}: {err}", file => 'foo.txt', err => $!,
  };

Developer:

  hurl 'Odd number of arguments passed to burf()' if @_ % 2;

=head1 Description

This module provides implements Sqitch exceptions. Exceptions may be thrown by
any part of the code, and, as long as a command is running, they will be
handled, showing the error message to the user.

=head1 Interface

=head2 Function

=head3 C<hurl>

Throws an exception. Pass the parameters as a hash reference, like so:



( run in 1.011 second using v1.01-cache-2.11-cpan-d8267643d1d )