Skip to content

add frame option #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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