Skip to content

Commit

Permalink
log when a filter is not found
Browse files Browse the repository at this point in the history
This adds a `log_die` helper which can be used to make sure a log is
generated in a helper when an exception might be captured.

Thanks @stephan48 for reporting this issue!

Fixes #126
  • Loading branch information
preaction committed Nov 15, 2020
1 parent 1c79049 commit a5c3be4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/Mojolicious/Plugin/Yancy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,11 @@ the code-refs.
Get or set the JSON schema for the given schema C<$name>. If no
schema name is given, returns a hashref of all the schema.
=head2 log_die
Raise an exception with L<Mojo::Exception/raise>, first logging
using L<Mojo::Log/fatal> (through the L<C<log> helper|Mojolicious::Plugin::DefaultHelpers/log>.
=head1 TEMPLATES
This plugin uses the following templates. To override these templates
Expand Down Expand Up @@ -700,6 +705,7 @@ sub register {
$app->helper( 'yancy.create' => \&_helper_create );
$app->helper( 'yancy.validate' => \&_helper_validate );
$app->helper( 'yancy.routify' => \&_helper_routify );
$app->helper( 'log_die' => \&_helper_log_die );

# Default form is Bootstrap4. Any form plugin added after this will
# override this one
Expand Down Expand Up @@ -992,7 +998,7 @@ sub _helper_filter_apply {
for my $filter ( @{ $prop_filters } ) {
( $filter, my @params ) = @$filter if ref $filter eq 'ARRAY';
my $sub = $filters->{ $filter };
die "Unknown filter: $filter (schema: $schema_name, field: $key)"
$c->log_die( "Unknown filter: $filter (schema: $schema_name, field: $key)" )
unless $sub;
$item = { %$item, $key => $sub->(
$key, $item->{ $key }, $schema->{properties}{ $key }, @params
Expand All @@ -1003,7 +1009,7 @@ sub _helper_filter_apply {
for my $filter ( @{ $schema_filters } ) {
( $filter, my @params ) = @$filter if ref $filter eq 'ARRAY';
my $sub = $filters->{ $filter };
die "Unknown filter: $filter (schema: $schema_name)"
$c->log_die( "Unknown filter: $filter (schema: $schema_name)" )
unless $sub;
$item = $sub->( $schema_name, $item, $schema, @params );
}
Expand Down Expand Up @@ -1048,4 +1054,19 @@ sub _helper_routify {
}
}

sub _helper_log_die {
my ( $self, $class, $err ) = @_;
# XXX: Handle JSON::Validator errors
if ( !$err ) {
$err = $class;
$class = 'Mojo::Exception';
}
if ( !$class->can( 'new' ) ) {
die $@ unless eval "package $class; use Mojo::Base 'Mojo::Exception'; 1";
}
my $e = $class->new( $err )->trace( 2 );
$self->log->fatal( $e );
die $e;
}

1;
16 changes: 16 additions & 0 deletions t/filter.t
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,20 @@ subtest 'run mask filter' => sub {
'filter on object is run';
};

subtest 'filter not found error' => sub {
$t->app->log->history([]);
local $t->app->yancy->config->{schema}{user}{properties}{email}{'x-filter'}[0] = [ 'DOES.NOT.EXIST' ];
my $user = {
username => 'filter',
email => '[email protected]',
password => 'unfiltered',
};
eval { $t->app->yancy->filter->apply( user => $user ) };
ok my $err = $@, 'error is thrown';
like $err, qr{Unknown filter: DOES\.NOT\.EXIST};
ok my $log = shift @{ $t->app->log->history }, 'log exists';
is $log->[1], 'fatal', 'log level is fatal';
like $log->[2], qr{Unknown filter: DOES\.NOT\.EXIST}, 'log message is correct';
};

done_testing;

0 comments on commit a5c3be4

Please sign in to comment.