Skip to content

Commit

Permalink
Stringify exceptions using Devel::PartialDump
Browse files Browse the repository at this point in the history
This is much friendlier than just stringifying refs. Anyone using structured
exceptions will thank you.
  • Loading branch information
rafl committed Apr 23, 2011
1 parent e44b710 commit b0ef4ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Expand Up @@ -9,6 +9,7 @@ requires 'strictures';
requires 'true';
requires 'Try::Tiny';
requires 'Sub::Exporter';
requires 'Devel::PartialDump';

test_requires 'Test::Most';

Expand Down
3 changes: 2 additions & 1 deletion lib/Try/Tiny/WarnCaught.pm
Expand Up @@ -3,6 +3,7 @@ our $VERSION = 0.01;
use strictures;
use true;
use Try::Tiny ();
use Devel::PartialDump 'warn';
use Sub::Exporter -setup => {
exports => [qw(catch)],
groups => {
Expand All @@ -13,7 +14,7 @@ use Sub::Exporter -setup => {
sub catch (&;@) {
my $cb = shift;
Try::Tiny::catch {
warn "Caught exception: $_";
warn "Caught exception: ", $_;
$cb->(@_);
};
}
Expand Down
34 changes: 24 additions & 10 deletions t/01_warn.t
Expand Up @@ -6,16 +6,30 @@ use Test::Most;
use Try::Tiny;
use Try::Tiny::WarnCaught;

my $should_warn = sub {
try {
die "hello world";
} catch {
1;
}
};
{
my $should_warn = sub {
try {
die "hello world";
} catch {
1;
}
};

warning_is { $should_warn->() } "Caught exception: hello world",
"catch block warns automatically now"
;
warning_is { $should_warn->() } "Caught exception: hello world",
"catch block warns automatically now";
}

{
my $should_warn = sub {
try {
die { foo => 42 };
} catch {
1;
}
};

warning_is { $should_warn->() } "Caught exception: { foo: 42 }",
"exceptions get dumped partially";
}

done_testing;

0 comments on commit b0ef4ea

Please sign in to comment.