Skip to content

Commit

Permalink
bring back throw method
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 26, 2016
1 parent 27d93f0 commit a33189a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

6.50 2016-02-27
- Added throw method to Mojo::Exception.

6.49 2016-02-26
- Removed throw method from Mojo::Exception.
Expand Down
13 changes: 12 additions & 1 deletion lib/Mojo/Exception.pm
Expand Up @@ -46,6 +46,8 @@ sub to_string {
return $str;
}

sub throw { CORE::die shift->new(shift)->trace(2)->inspect }

sub trace {
my ($self, $start) = (shift, shift // 1);
my @frames;
Expand Down Expand Up @@ -96,7 +98,7 @@ Mojo::Exception - Exceptions with context
use Mojo::Exception;
# Throw exception and show stack trace
eval { die Mojo::Exception->new('Something went wrong!')->trace };
eval { Mojo::Exception->throw('Something went wrong!') };
say "$_->[1]:$_->[2]" for @{$@->frames};
# Customize exception
Expand Down Expand Up @@ -189,6 +191,15 @@ Render exception.
# Render exception with context
say $e->verbose(1)->to_string;
=head2 throw
Mojo::Exception->throw('Something went wrong!');
Throw exception from the current execution context.
# Longer version
die Mojo::Exception->new('Something went wrong!')->trace->inspect;
=head2 trace
$e = $e->trace;
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojolicious.pm
Expand Up @@ -185,9 +185,8 @@ sub startup { }

sub _exception {
my ($next, $c) = @_;
local $SIG{__DIE__} = sub {
CORE::die ref $_[0] ? $_[0] : Mojo::Exception->new(@_)->trace->inspect;
};
local $SIG{__DIE__}
= sub { ref $_[0] ? CORE::die $_[0] : Mojo::Exception->throw(@_) };
$c->helpers->reply->exception($@) unless eval { $next->(); 1 };
}

Expand Down
5 changes: 2 additions & 3 deletions t/mojo/exception.t
Expand Up @@ -16,7 +16,7 @@ eval {

# test

my $wrapper = sub { die Mojo::Exception->new('Works!')->trace->inspect };
my $wrapper = sub { Mojo::Exception->throw('Works!') };
$wrapper->();

# test
Expand All @@ -36,8 +36,7 @@ is $e->lines_before->[3][0], 18, 'right number';
ok !$e->lines_before->[3][1], 'empty line';
is $e->lines_before->[4][0], 19, 'right number';
is $e->lines_before->[4][1],
" my \$wrapper = sub { die Mojo::Exception->new('Works!')->trace->inspect };",
'right line';
" my \$wrapper = sub { Mojo::Exception->throw('Works!') };", 'right line';
is $e->line->[0], 20, 'right number';
is $e->line->[1], " \$wrapper->();", 'right line';
is $e->lines_after->[0][0], 21, 'right number';
Expand Down

0 comments on commit a33189a

Please sign in to comment.