App::Sqitch::X3pm
Sqitch exception class
Usage
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;
Overview
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.
Interface
Function
"hurl"
Throws an exception. Pass the parameters as a hash reference, like so:
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 => $!,};
More simply, if all you need to pass are the "ident" and "message" parameters, you can pass them as the only arguments to hurl():
open my $fh, '>', 'foo.txt'or hurl io => __x 'Cannot open {file}: {err}", file => 'foo.txt', err => $!
For errors that should only happen during development (e.g., an invalid parameter passed by some other library that should know better), you can omit the "ident":
hurl 'Odd number of arguments passed to burf()' if @_ % 2;
In this case, the "ident" will be "DEV", which you should not otherwise use. Sqitch will emit a more detailed error message, including a stack trace, when it sees "DEV" exceptions.
The supported parameters are:
- "ident"
-
A non-localized string identifying the type of exception.
- "message"
-
The exception message. Use Locale::TextDomain to craft localized messages.
- "exitval"
-
Suggested exit value to use. Defaults to 2. This will be used if Sqitch handles an exception while a command is running.
Methods
"as_string"
my $errstr = $x->as_string;
Returns the stringified representation of the exception. This value is also used for string overloading of the exception object, which means it is the output shown for uncaught exceptions. Its contents are the concatenation of the exception message, the previous exception (if any), and the stack trace.
"details_string"
my $errstr = $x->details;
Returns the the stringified version of the previous exception (if any), and the stack trace.
Handling Exceptions
use Try::Tiny to do exception handling, like so:
use Try::Tiny;try {# ...} catch {die $_ unless eval { $_->isa('App::Sqitch::X') };$sqitch->vent($x_->message);if ($_->ident eq 'DEV') {$sqitch->vent($_->stack_trace->as_string);} else {$sqitch->debug($_->stack_trace->as_string);}exit $_->exitval;};
Use the "ident" attribute to determine what category of exception it is, and take changes as appropriate.
Releases
6 releases since Debian 8 (Jessie); the text changed 5 times.