Bug 27806 - Exceptions thrown during a call to ->render are not caught and passed to ->unhandled_exception
Summary: Exceptions thrown during a call to ->render are not caught and passed to ->un...
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: REST API (show other bugs)
Version: master
Hardware: All All
: P5 - low enhancement (vote)
Assignee: Bugs List
QA Contact:
URL:
Keywords:
Depends on: 27680
Blocks:
  Show dependency treegraph
 
Reported: 2021-02-26 13:27 UTC by Martin Renvoize
Modified: 2021-03-22 17:42 UTC (History)
2 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:


Attachments
Bug 27806: (follow-up) Add Koha::Exception for bad queries (2.82 KB, patch)
2021-02-26 13:29 UTC, Martin Renvoize
Details | Diff | Splinter Review
Bug 27806: (follow-up) Attempt to catch the exception (1.79 KB, patch)
2021-02-26 13:29 UTC, Martin Renvoize
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Renvoize 2021-02-26 13:27:50 UTC
Whilst working on 27680 I discovered that we do not catch exceptions that are thrown at the point of render.

I failed to resolve the problem there so have moved this piece to it's own bug.

The tests added catch the 500 responses and those responses are the HTML rendered Mojo::Exception templates..

I'll attach the patches I came up with that are not working as expected :(.

I've also tried using

$self->helper( 'reply.exception' => sub { my ($page, $c, $e) = @_; # Custom error page handling here } );

and

$self->before_render => sub {

  my ($c, $args) = @_;

  # Make sure we are rendering the exception template
  return unless my $template = $args->{template};
  return unless $template eq 'exception';

  # Switch to JSON rendering if content negotiation allows it
  return unless $c->accepts('json');
  $args->{json} = {exception => $c->stash('exception')};
};

and neither appeared to work :(
Comment 1 Martin Renvoize 2021-02-26 13:29:10 UTC
Created attachment 117376 [details] [review]
Bug 27806: (follow-up) Add Koha::Exception for bad queries

This patch adds a PropertyNotFound exception tothe 'as_list' method to
catch DBIx::Class excpetions caused by passing bad column names into a
query.
Comment 2 Martin Renvoize 2021-02-26 13:29:13 UTC
Created attachment 117377 [details] [review]
Bug 27806: (follow-up) Attempt to catch the exception

Help: I'm struggling to catch the point at which the exception should be
thrown to convert it to a 400 error response in the API. '->search' does
not actually trigger the DB hit until we reference some data from it..
so we can't use that.. so I tried '->count', but of course.. that drops
'order_by' which is the failure in this case and so we pass through.