| 1 | package ForgeOps::Tracker::Integrations::PSGI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use parent 'Plack::Middleware'; | ||
| 6 | use ForgeOps::Tracker; | ||
| 7 | |||
| 8 | # PSGI/Plack middleware: works under any PSGI-speaking framework (plain PSGI apps, Dancer2 via | ||
| 9 | # its own Plack integration, Mojolicious under Plack::Handler::???). Wrap your app with it: | ||
| 10 | # | ||
| 11 | # use Plack::Builder; | ||
| 12 | # builder { | ||
| 13 | # enable '+ForgeOps::Tracker::Integrations::PSGI'; # '+': see Plack::Builder's own docs on | ||
| 14 | # # enable(): without it, a bare name is | ||
| 15 | # # looked up under Plack::Middleware::* | ||
| 16 | # # instead of taken as an exact class name. | ||
| 17 | # $app; | ||
| 18 | # }; | ||
| 19 | # | ||
| 20 | # Wraps the downstream app call in an eval, reports, then re-raises the *original* exception | ||
| 21 | # unchanged: so Plack's own error handling (Plack::Middleware::StackTrace in development, or the | ||
| 22 | # PSGI server's own 500 response in production) continues exactly as if this middleware weren't | ||
| 23 | # there. Only an exception the app itself catches and handles is invisible to this, same as every | ||
| 24 | # other framework integration in this repo. | ||
| 25 | sub call { | ||
| 26 | my ($self, $env) = @_; | ||
| 27 | |||
| 28 | # A fresh breadcrumb trail per request: a prefork worker serves many requests in a row. | ||
| 29 | ForgeOps::Tracker::clear_breadcrumbs(); | ||
| 30 | |||
| 31 | my @response = eval { @{ $self->app->($env) } }; | ||
| 32 | if (my $error = $@) { | ||
| 33 | ForgeOps::Tracker::report($error, { | ||
| 34 | path => $env->{PATH_INFO}, | ||
| 35 | method => $env->{REQUEST_METHOD}, | ||
| 36 | }); | ||
| 37 | die $error; | ||
| 38 | } | ||
| 39 | |||
| 40 | return \@response; | ||
| 41 | } | ||
| 42 | |||
| 43 | 1; |