Skip to content

Commit

Permalink
add frame option
Browse files Browse the repository at this point in the history
  • Loading branch information
plicease committed Oct 11, 2022
1 parent 036c24e commit e75bb05
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Revision history for {{$dist->name}}

{{$NEXT}}
- Documentation improvements (gh#11)
- Added frame option to throw (gh#12)

0.02 2022-10-06 13:35:58 -0600
- Import will croak if you provide a bad option (gh#6, gh#7)
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,19 @@ The base class provides these attributes and methods:
## throw

```perl
Exception::FFI::ErrorCode::Base->throw( code => $code );
Exception::FFI::ErrorCode::Base->throw( code => $code, %attr );
Exception::FFI::ErrorCode::Base->throw( code => $code, frame => $frame, %attr );
```

Throws the exception with the given code. Obviously you would throw the subclass, not the
base class.

If you have added additional attributes via [Class::Tiny](https://metacpan.org/pod/Class::Tiny) you can provide them as
`%attr`.

If you want the exception to appear to happen from a different frame then you can
specify it with `$frame`.

## strerror

```perl
Expand Down
15 changes: 11 additions & 4 deletions lib/Exception/FFI/ErrorCode.pm
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,18 @@ The base class provides these attributes and methods:
=head2 throw
Exception::FFI::ErrorCode::Base->throw( code => $code );
Exception::FFI::ErrorCode::Base->throw( code => $code, %attr );
Exception::FFI::ErrorCode::Base->throw( code => $code, frame => $frame, %attr );
Throws the exception with the given code. Obviously you would throw the subclass, not the
base class.
If you have added additional attributes via L<Class::Tiny> you can provide them as
C<%attr>.
If you want the exception to appear to happen from a different frame then you can
specify it with C<$frame>.
=head2 strerror
my $string = $ex->strerror;
Expand Down Expand Up @@ -299,9 +306,9 @@ and attached to all exceptions managed by L<Exception::FFI::ErrorCode>.
},
bool => sub { 1 }, fallback => 1;

sub throw ($proto, @rest)
sub throw ($proto, %rest)
{
my($package, $filename, $line) = caller;
my($package, $filename, $line) = caller( delete $rest{frame} // 0 );

my $self;
if(is_blessed_ref $proto)
Expand All @@ -314,7 +321,7 @@ and attached to all exceptions managed by L<Exception::FFI::ErrorCode>.
else
{
$self = $proto->new(
@rest,
%rest,
package => $package,
filename => $filename,
line => $line,
Expand Down

0 comments on commit e75bb05

Please sign in to comment.