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

Bot::Cobalt::Error - Lightweight error objects

SYNOPSIS

package SomePackage;

sub some_method {
  . . .
  
  die Bot::Cobalt::Error->new(
    "Some errors occured:",
    @errors
  )->join("\n");

  ## ... same as:
  die Bot::Cobalt::Error->new(
    "Some errors occured:\n",
    join("\n", @errors)
  );
}


package CallerPackage;

use Try::Tiny;

try {
  SomePackage->some_method();
} catch {
  ## $error isa Bot::Cobalt::Error
  my $error = $_;
  
  ## Stringifies to the error string:
  warn "$error\n";
};

DESCRIPTION

A lightweight exception object for Bot::Cobalt.

new() takes a list of messages used to compose an error string.

The objects themselves stringify to the concatenated stored errors.

A Devel::StackTrace instance is created at construction time; it is accessible via "trace".

string

Returns the current error string; this is the same value returned when the object is stringified, such as:

warn "$error\n";

join

$error = $error->join("\n");

Returns a new object whose only element is the result of joining the stored list of errors with the specified expression.

Defaults to joining with a single space. Does not modify the existing object.

push

$error = $error->push(@errors);

Appends the specified list to the existing array of errors.

Modifies and returns the existing object.

slice

$error = $error->slice(0 .. 2);

Returns a new object whose elements are as specified. Does not modify the existing object.

throw

my $err = Bot::Cobalt::Error->new;
$err->push( @errors );
$err->throw

Throw an exception by calling die() with the current object. The Devel::StackTrace object is reinstanced from where throw() is called (see "trace").

trace

## Stack trace as string:
warn $error->trace->as_string;

A Devel::StackTrace instance; see Devel::StackTrace.

unshift

$error = $error->unshift(@errors);

Prepends the specified list to the existing array of errors.

Modifies and returns the existing object.

AUTHOR

Jon Portnoy <avenj@cobaltirc.org>