Skip to content

Commit 1ddfc13

Browse files
committed
Support setting all attributes using "throw"
1 parent b2dc7de commit 1ddfc13

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

lib/Data/Object/Exception.pm

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,23 @@ fun BUILDARGS($class, @args) {
6464
# FUNCTIONS
6565

6666
fun throw($self, $message, $context, $offset) {
67-
my $class = ref $self || $self;
68-
6967
my $id;
70-
my $frames;
68+
69+
my $class = ref $self || $self;
7170

7271
my $args = {};
7372

73+
if (ref $message eq 'ARRAY') {
74+
($id, $message) = @$message;
75+
}
76+
7477
if (ref $self) {
7578
for my $name (keys %$self) {
7679
$args->{$name} = $self->{$name};
7780
}
7881
}
7982

83+
$args->{id} = $id if $id;
8084
$args->{message} = $message if $message;
8185
$args->{context} = $context if $context;
8286

t/Data_Object_Exception.t

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,36 @@ The package allows objects to be instantiated with key-value arguments.
8282

8383
=method throw
8484
85-
The throw method throws an error with message.
85+
The throw method throws an error with message (and optionally, an ID).
8686
8787
=signature throw
8888
89-
throw(Str $class, Any $context, Maybe[Number] $offset) : Any
89+
throw(Tuple[Str, Str] | Str $message, Any $context, Maybe[Number] $offset) : Any
9090
9191
=example-1 throw
9292
9393
use Data::Object::Exception;
9494
95+
my $exception = Data::Object::Exception->new;
96+
97+
$exception->throw('Oops!')
98+
99+
=example-2 throw
100+
101+
use Data::Object::Exception;
102+
95103
my $exception = Data::Object::Exception->new('Oops!');
96104
97105
$exception->throw
98106
107+
=example-3 throw
108+
109+
use Data::Object::Exception;
110+
111+
my $exception = Data::Object::Exception->new;
112+
113+
$exception->throw(['E001', 'Oops!'])
114+
99115
=cut
100116

101117
=method explain
@@ -204,6 +220,34 @@ $subs->example(-1, 'throw', 'method', fun($tryable) {
204220
$result
205221
});
206222

223+
$subs->example(-2, 'throw', 'method', fun($tryable) {
224+
$tryable->default(fun($caught) {
225+
$caught
226+
});
227+
ok my $result = $tryable->result;
228+
ok $result->isa('Data::Object::Exception');
229+
ok !length $result->id;
230+
ok !length $result->context;
231+
ok length $result->frames;
232+
is $result->message, 'Oops!';
233+
234+
$result
235+
});
236+
237+
$subs->example(-3, 'throw', 'method', fun($tryable) {
238+
$tryable->default(fun($caught) {
239+
$caught
240+
});
241+
ok my $result = $tryable->result;
242+
ok $result->isa('Data::Object::Exception');
243+
is $result->id, 'E001';
244+
ok !length $result->context;
245+
ok length $result->frames;
246+
is $result->message, 'Oops!';
247+
248+
$result
249+
});
250+
207251
$subs->example(-1, 'explain', 'method', fun($tryable) {
208252
ok my $result = $tryable->result;
209253

0 commit comments

Comments
 (0)