Skip to content

Instantly share code, notes, and snippets.

@itsgoingd
Last active August 29, 2015 14:01
Show Gist options
  • Save itsgoingd/86085598fb414b69a06c to your computer and use it in GitHub Desktop.
Save itsgoingd/86085598fb414b69a06c to your computer and use it in GitHub Desktop.
Clockwork Laravel exception handler
<?php
use Illuminate\Http\Response;
use Clockwork\Clockwork;
use Clockwork\Support\Laravel\ClockworkServiceProvider;
App::error(function(Exception $exception)
{
$app = app();
$output = $app['whoops']->handleException($exception);
$service = new ClockworkServiceProvider($app);
if (!$service->isCollectingData()) {
return; // Collecting data is disabled, return immediately
}
// don't collect data for configured URIs
$request_uri = $app['request']->getRequestUri();
$filter_uris = $app['config']->get('clockwork::config.filter_uris', array());
$filter_uris[] = '/__clockwork/[0-9\.]+'; // don't collect data for Clockwork requests
foreach ($filter_uris as $uri) {
$regexp = '#' . str_replace('#', '\#', $uri) . '#';
if (preg_match($regexp, $request_uri)) {
return;
}
}
$app['clockwork.laravel']->setResponse(new Response('', 500));
$app['clockwork']->resolveRequest();
$app['clockwork']->storeRequest();
if (!$service->isEnabled()) {
return; // Clockwork is disabled, don't set the headers
}
$headers = array(
'X-Clockwork-Id' => $app['clockwork']->getRequest()->id,
'X-Clockwork-Version' => Clockwork::VERSION
);
if ($app['request']->getBasePath()) {
$headers['X-Clockwork-Path'] = $app['request']->getBasePath() . '/__clockwork/';
}
$extra_headers = $app['config']->get('clockwork::config.headers');
if ($extra_headers && is_array($extra_headers)) {
foreach ($extra_headers as $header_name => $header_value) {
$headers['X-Clockwork-Header-' . $header_name] = $header_value;
}
}
return new Response($output, 500, $headers);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment