PerlDancer / Dancer2

Perl Dancer Next Generation (rewrite of Perl Dancer)
http://perldancer.org/
Other
542 stars 274 forks source link

REST and Exceptions, croaking and context loss in before hook #1632

Open yent opened 2 years ago

yent commented 2 years ago

Hello all,

We are trying to use Dancer2 to add a REST API to the Sympa software and we are facing a bit of an issue ... Actually 2 issues.

We use Throwable based exceptions everywhere we can.

Our idea is to use a "before" hook to do some stuff before every call, like analyzing some specific authentication headers and such.

If anything is "thrown" within our hook it is caught and turned into a string (croak in App.pm@1185), so when our "before_error" gets the error we don't have our "complex" exception anymore ... This is a bit of a problem as the original exceptions contain stuff that should be sent back to the client ... Any idea on how to have a Throwable bubble up to the Dancer Error ?

The other issue, we use the REST plugin to handle response formats, problem is, when an exception happens within a hook $app->cleanup runs (App.pm@1183) which trashes the request so the REST plugin loses it's content-type header and the response type then defaults to html, when the client expects (and requested) json ...

But maybe we are not doing things the right way, basically we need to have some code that runs before every route dispatching, code that may throw "complex" exceptions, and have those exceptions sent back to the client in the format it requested, any idea on how to acheive that ?

Thanks in advance