Skip to content

Commit

Permalink
Refactor collector exceptions (#1522)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 authored Feb 14, 2024
1 parent 09d3dc7 commit 0f74197
Showing 1 changed file with 60 additions and 129 deletions.
189 changes: 60 additions & 129 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function () use ($debugbar, $startTime) {
$this->app['config']->get('debugbar.options.exceptions.chain', true)
);
$this->addCollector($exceptionCollector);
} catch (\Exception $e) {
} catch (Exception $e) {
}
}

Expand All @@ -224,14 +224,8 @@ function () use ($debugbar, $startTime) {
$startTime = $this->app['request']->server('REQUEST_TIME_FLOAT');
$this->addCollector(new EventCollector($startTime));
$this->app['events']->subscribe($debugbar['event']);
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add EventCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add EventCollector', $e);
}
}

Expand All @@ -250,28 +244,16 @@ function ($view, $data = []) use ($debugbar) {
$debugbar['views']->addView($view);
}
);
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add ViewCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add ViewCollector', $e);
}
}

if (!$this->isLumen() && $this->shouldCollect('route')) {
try {
$this->addCollector($this->app->make('Barryvdh\Debugbar\DataCollector\RouteCollector'));
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add RouteCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add RouteCollector', $e);
}
}

Expand All @@ -298,7 +280,7 @@ function ($level, $message = null, $context = null) use ($logger) {
} else {
$logMessage = "[INVALID UTF-8 DATA]";
}
} catch (\Exception $e) {
} catch (Exception $e) {
$logMessage = "[Exception: " . $e->getMessage() . "]";
}
$logger->addMessage(
Expand All @@ -311,14 +293,8 @@ function ($level, $message = null, $context = null) use ($logger) {
} else {
$this->addCollector(new MonologCollector($this->getMonologLogger()));
}
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add LogsCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add LogsCollector', $e);
}
}

Expand Down Expand Up @@ -383,14 +359,8 @@ function (\Illuminate\Database\Events\QueryExecuted $query) {
}
}
);
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add listen to Queries for Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot listen to Queries', $e);
}

try {
Expand Down Expand Up @@ -447,14 +417,8 @@ function (\Illuminate\Database\Events\ConnectionEstablished $event) use ($queryC
}
}
);
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add listen transactions to Queries for Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot listen transactions to Queries', $e);
}
}

Expand All @@ -466,19 +430,17 @@ function (\Illuminate\Database\Events\ConnectionEstablished $event) use ($queryC
$this['models']->countClass($model);
}
});
} catch (\Exception $e) {
// No Models collector
} catch (Exception $e) {
$this->addCollectorException('Cannot add Models Collector', $e);
}
}

if ($this->shouldCollect('livewire', true) && $this->app->bound('livewire')) {
try {
$livewireCollector = $this->app->make('Barryvdh\Debugbar\DataCollector\LivewireCollector');
$this->addCollector($livewireCollector);
} catch (\Exception $e) {
$this->addThrowable(
new Exception('Cannot add Livewire Collector: ' . $e->getMessage(), $e->getCode(), $e)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add Livewire Collector', $e);
}
}

Expand Down Expand Up @@ -524,29 +486,17 @@ public function __toString(): string
}
});
}
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add MailCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add SymfonyMailCollector', $e);
}
}

if ($this->shouldCollect('logs', false)) {
try {
$file = $this->app['config']->get('debugbar.options.logs.file');
$this->addCollector(new LogsCollector($file));
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add LogsCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add LogsCollector', $e);
}
}
if ($this->shouldCollect('files', false)) {
Expand All @@ -562,23 +512,17 @@ public function __toString(): string
$this->app['config']->get('debugbar.options.auth.show_name')
);
$this->addCollector($authCollector);
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add AuthCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add AuthCollector', $e);
}
}

if ($this->shouldCollect('gate', false)) {
try {
$gateCollector = $this->app->make('Barryvdh\Debugbar\DataCollector\GateCollector');
$this->addCollector($gateCollector);
} catch (\Exception $e) {
// No Gate collector
} catch (Exception $e) {
$this->addCollectorException('Cannot add GateCollector', $e);
}
}

Expand All @@ -589,14 +533,8 @@ public function __toString(): string
$cacheCollector = new CacheCollector($startTime, $collectValues);
$this->addCollector($cacheCollector);
$this->app['events']->subscribe($cacheCollector);
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add CacheCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add CacheCollector', $e);
}
}

Expand All @@ -606,8 +544,8 @@ public function __toString(): string
$this->app['events']->listen(\Illuminate\Queue\Events\JobQueued::class, function ($event) {
$this['jobs']->countClass($event->job);
});
} catch (\Exception $e) {
// No Jobs collector
} catch (Exception $e) {
$this->addCollectorException('Cannot add Jobs Collector', $e);
}
}

Expand Down Expand Up @@ -696,7 +634,7 @@ public function stopMeasure($name)
$collector = $this->getCollector('time');
try {
$collector->stopMeasure($name);
} catch (\Exception $e) {
} catch (Exception $e) {
// $this->addThrowable($e);
}
}
Expand Down Expand Up @@ -727,6 +665,23 @@ public function addThrowable($e)
}
}

/**
* Register collector exceptions
*
* @param string $message
* @param Exception $exception
*/
protected function addCollectorException(string $message, Exception $exception)
{
$this->addThrowable(
new Exception(
$message . ' on Laravel Debugbar: ' . $exception->getMessage(),
$exception->getCode(),
$exception
)
);
}

/**
* Returns a JavascriptRenderer for this instance
*
Expand Down Expand Up @@ -766,14 +721,8 @@ public function modifyResponse(Request $request, Response $response)
$configCollector = new ConfigCollector();
$configCollector->setData($app['config']->all());
$this->addCollector($configCollector);
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add ConfigCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add ConfigCollector', $e);
}
}

Expand All @@ -788,14 +737,8 @@ public function modifyResponse(Request $request, Response $response)
if ($this->shouldCollect('session') && ! $this->hasCollector('session')) {
try {
$this->addCollector(new SessionCollector($sessionManager, $sessionHiddens));
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add SessionCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add SessionCollector', $e);
}
}
} else {
Expand All @@ -810,28 +753,16 @@ public function modifyResponse(Request $request, Response $response)
try {
$reqId = $this->getCurrentRequestId();
$this->addCollector(new RequestCollector($request, $response, $sessionManager, $reqId, $requestHiddens));
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add SymfonyRequestCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add SymfonyRequestCollector', $e);
}
}

if ($app['config']->get('debugbar.clockwork') && ! $this->hasCollector('clockwork')) {
try {
$this->addCollector(new ClockworkCollector($request, $response, $sessionManager, $requestHiddens));
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add ClockworkCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
} catch (Exception $e) {
$this->addCollectorException('Cannot add ClockworkCollector', $e);
}

$this->addClockworkHeaders($response);
Expand All @@ -840,7 +771,7 @@ public function modifyResponse(Request $request, Response $response)
if ($response->isRedirection()) {
try {
$this->stackData();
} catch (\Exception $e) {
} catch (Exception $e) {
$app['log']->error('Debugbar exception: ' . $e->getMessage());
}
} elseif (
Expand All @@ -853,7 +784,7 @@ public function modifyResponse(Request $request, Response $response)
if ($app['config']->get('debugbar.add_ajax_timing', false)) {
$this->addServerTimingHeaders($response);
}
} catch (\Exception $e) {
} catch (Exception $e) {
$app['log']->error('Debugbar exception: ' . $e->getMessage());
}
} elseif (
Expand All @@ -867,13 +798,13 @@ public function modifyResponse(Request $request, Response $response)
try {
// Just collect + store data, don't inject it.
$this->collect();
} catch (\Exception $e) {
} catch (Exception $e) {
$app['log']->error('Debugbar exception: ' . $e->getMessage());
}
} else {
try {
$this->injectDebugbar($response);
} catch (\Exception $e) {
} catch (Exception $e) {
$app['log']->error('Debugbar exception: ' . $e->getMessage());
}
}
Expand Down

0 comments on commit 0f74197

Please sign in to comment.