NAME

Bitcoin::Crypto::Exception - Exception classes for Bitcoin::Crypto

SYNOPSIS

try {
	decode_segwit('Not a segwit address');
}
catch ($error) {
	# $error is an instance of Bitcoin::Crypto::Exception and stringifies automatically
	warn "$error";

	# it also contains some information about the problem to avoid regex matching
	if ($error->isa('Bitcoin::Crypto::Exception::Bech32InputFormat')) {
		log $error->message;
	}
}

DESCRIPTION

An exception wrapper class with automatic stringification and standarized raising.

Contains inline packages that identify parts that went wrong (like Bitcoin::Crypto::Exception::Sign for errors in signature generation). Search individual Bitcoin::Crypto packages documentation for a list the exception classes to check for extra control flow when needed.

INTERFACE

Attributes

message

The wrapped error message (a string). Note: this is the raw message, not the serialized form like in "as_string".

caller

Not assignable in the constructor

An array ref containing: package name, file name and line number (same as [caller()] perl expression). It will point to the first place from outside Bitcoin::Crypto which called it. May be undefined if it cannot find a calling source.

Methods

new

$runner = Bitcoin::Crypto::Exception->new(%data)

This is a standard Moo constructor, which can be used to create the object. It takes arguments specified in "Attributes". For exceptions, it's probably better to use "raise" instead.

Returns class instance.

as_string

$error_info = $object->as_string()

Stringifies the error, using the "message" method, "caller" method and some extra text for context.

raise

$object->raise()
$class->raise($message)

Creates a new instance and throws it. If used on an object, throws it right away.

try {
	# throws, but will be catched
	Bitcoin::Crypto::Exception->raise('something went wrong');
}
catch ($exception) {
	# throws again
	$exception->raise;
}

throw

An alias to raise.

trap_into

$sub_result = $class->trap_into($sub, $prefix)

Executes the given subroutine in an exception-trapping environment. Any exceptions thrown inside the subroutine $sub will be re-thrown after turning them into objects of the given ::Exception class. If no exception is thrown, method returns the value returned by $sub.

my $result = Bitcoin::Crypto::Exception->trap_into(sub {
	die 'something went wrong';
});

$prefix can be specified to better format the message.