Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/Specio/Constraint/Role/Interface.pm
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ sub has_coercions { scalar keys %{ $_[0]->{_coercions} } }
sub validate_or_die {
my $self = shift;
my $value = shift;
my $context = shift;

return if $self->value_is_valid($value);

Specio::Exception->throw(
message => $self->_message_generator->($value),
type => $self,
value => $value,
($context ? (context => $context) : ()),
);
}

Expand Down Expand Up @@ -381,13 +383,17 @@ sub inline_coercion_and_check {

sub inline_assert {
my $self = shift;
my $context = shift;

my $type_var_name = '$_Specio_Constraint_Interface_type' . $counter;
my $message_generator_var_name
= '$_Specio_Constraint_Interface_message_generator' . $counter;
my $context_var_name
= '$_Specio_Constraint_Interface_context' . $counter;
my %env = (
$type_var_name => \$self,
$message_generator_var_name => \( $self->_message_generator ),
($context ? ($context_var_name => \$context) : ()),
%{ $self->inline_environment },
);

Expand All @@ -396,7 +402,8 @@ sub inline_coercion_and_check {
$source .= $self->_inline_throw_exception(
$_[0],
$message_generator_var_name,
$type_var_name
$type_var_name,
($context ? $context_var_name : ()),
);
$source .= ';';

Expand Down Expand Up @@ -445,12 +452,15 @@ sub _inline_throw_exception {
my $value_var = shift;
my $message_generator_var_name = shift;
my $type_var_name = shift;
my $context_var_name = shift;

#<<<
return 'Specio::Exception->throw( '
. ' message => ' . $message_generator_var_name . '->(' . $value_var . '),'
. ' type => ' . $type_var_name . ','
. ' value => ' . $value_var . ' )';
. ' value => ' . $value_var . ','
. ($context_var_name ? ' context => ' . $context_var_name . ',' : '')
. ')';
#>>>
}

Expand Down
9 changes: 9 additions & 0 deletions lib/Specio/Exception.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ use Specio::OO;
stack_trace => {
init_arg => undef,
},
context => {
isa => 'HashRef',
},
};

## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
Expand All @@ -50,6 +53,12 @@ sub as_string {
my $self = shift;

my $str = $self->message;
if ($self->context and my %context = %{$self->context}) {
$str
.= ' ('
. join(', ', map { "$_: $context{$_}" } sort keys %context)
. ')';
}
$str .= "\n\n" . $self->stack_trace->as_string;

return $str;
Expand Down
10 changes: 10 additions & 0 deletions t/exception.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ use Specio::Library::Builtins;
qr/Validation failed for type named Str .+ with value \[\s*\]/,
'exception contains expected error'
);

$e = exception {
$str->validate_or_die( [], { attribute => 'foo' } );
};

like(
$e->as_string,
qr/Validation failed for type named Str .+ with value \[\s*\] \(attribute: foo\)/,
'exception contains expected error'
);
}

done_testing();