Skip to content

Commit 0f74197

Browse files
authored
Refactor collector exceptions (#1522)
1 parent 09d3dc7 commit 0f74197

File tree

1 file changed

+60
-129
lines changed

1 file changed

+60
-129
lines changed

src/LaravelDebugbar.php

Lines changed: 60 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function () use ($debugbar, $startTime) {
207207
$this->app['config']->get('debugbar.options.exceptions.chain', true)
208208
);
209209
$this->addCollector($exceptionCollector);
210-
} catch (\Exception $e) {
210+
} catch (Exception $e) {
211211
}
212212
}
213213

@@ -224,14 +224,8 @@ function () use ($debugbar, $startTime) {
224224
$startTime = $this->app['request']->server('REQUEST_TIME_FLOAT');
225225
$this->addCollector(new EventCollector($startTime));
226226
$this->app['events']->subscribe($debugbar['event']);
227-
} catch (\Exception $e) {
228-
$this->addThrowable(
229-
new Exception(
230-
'Cannot add EventCollector to Laravel Debugbar: ' . $e->getMessage(),
231-
$e->getCode(),
232-
$e
233-
)
234-
);
227+
} catch (Exception $e) {
228+
$this->addCollectorException('Cannot add EventCollector', $e);
235229
}
236230
}
237231

@@ -250,28 +244,16 @@ function ($view, $data = []) use ($debugbar) {
250244
$debugbar['views']->addView($view);
251245
}
252246
);
253-
} catch (\Exception $e) {
254-
$this->addThrowable(
255-
new Exception(
256-
'Cannot add ViewCollector to Laravel Debugbar: ' . $e->getMessage(),
257-
$e->getCode(),
258-
$e
259-
)
260-
);
247+
} catch (Exception $e) {
248+
$this->addCollectorException('Cannot add ViewCollector', $e);
261249
}
262250
}
263251

264252
if (!$this->isLumen() && $this->shouldCollect('route')) {
265253
try {
266254
$this->addCollector($this->app->make('Barryvdh\Debugbar\DataCollector\RouteCollector'));
267-
} catch (\Exception $e) {
268-
$this->addThrowable(
269-
new Exception(
270-
'Cannot add RouteCollector to Laravel Debugbar: ' . $e->getMessage(),
271-
$e->getCode(),
272-
$e
273-
)
274-
);
255+
} catch (Exception $e) {
256+
$this->addCollectorException('Cannot add RouteCollector', $e);
275257
}
276258
}
277259

@@ -298,7 +280,7 @@ function ($level, $message = null, $context = null) use ($logger) {
298280
} else {
299281
$logMessage = "[INVALID UTF-8 DATA]";
300282
}
301-
} catch (\Exception $e) {
283+
} catch (Exception $e) {
302284
$logMessage = "[Exception: " . $e->getMessage() . "]";
303285
}
304286
$logger->addMessage(
@@ -311,14 +293,8 @@ function ($level, $message = null, $context = null) use ($logger) {
311293
} else {
312294
$this->addCollector(new MonologCollector($this->getMonologLogger()));
313295
}
314-
} catch (\Exception $e) {
315-
$this->addThrowable(
316-
new Exception(
317-
'Cannot add LogsCollector to Laravel Debugbar: ' . $e->getMessage(),
318-
$e->getCode(),
319-
$e
320-
)
321-
);
296+
} catch (Exception $e) {
297+
$this->addCollectorException('Cannot add LogsCollector', $e);
322298
}
323299
}
324300

@@ -383,14 +359,8 @@ function (\Illuminate\Database\Events\QueryExecuted $query) {
383359
}
384360
}
385361
);
386-
} catch (\Exception $e) {
387-
$this->addThrowable(
388-
new Exception(
389-
'Cannot add listen to Queries for Laravel Debugbar: ' . $e->getMessage(),
390-
$e->getCode(),
391-
$e
392-
)
393-
);
362+
} catch (Exception $e) {
363+
$this->addCollectorException('Cannot listen to Queries', $e);
394364
}
395365

396366
try {
@@ -447,14 +417,8 @@ function (\Illuminate\Database\Events\ConnectionEstablished $event) use ($queryC
447417
}
448418
}
449419
);
450-
} catch (\Exception $e) {
451-
$this->addThrowable(
452-
new Exception(
453-
'Cannot add listen transactions to Queries for Laravel Debugbar: ' . $e->getMessage(),
454-
$e->getCode(),
455-
$e
456-
)
457-
);
420+
} catch (Exception $e) {
421+
$this->addCollectorException('Cannot listen transactions to Queries', $e);
458422
}
459423
}
460424

@@ -466,19 +430,17 @@ function (\Illuminate\Database\Events\ConnectionEstablished $event) use ($queryC
466430
$this['models']->countClass($model);
467431
}
468432
});
469-
} catch (\Exception $e) {
470-
// No Models collector
433+
} catch (Exception $e) {
434+
$this->addCollectorException('Cannot add Models Collector', $e);
471435
}
472436
}
473437

474438
if ($this->shouldCollect('livewire', true) && $this->app->bound('livewire')) {
475439
try {
476440
$livewireCollector = $this->app->make('Barryvdh\Debugbar\DataCollector\LivewireCollector');
477441
$this->addCollector($livewireCollector);
478-
} catch (\Exception $e) {
479-
$this->addThrowable(
480-
new Exception('Cannot add Livewire Collector: ' . $e->getMessage(), $e->getCode(), $e)
481-
);
442+
} catch (Exception $e) {
443+
$this->addCollectorException('Cannot add Livewire Collector', $e);
482444
}
483445
}
484446

@@ -524,29 +486,17 @@ public function __toString(): string
524486
}
525487
});
526488
}
527-
} catch (\Exception $e) {
528-
$this->addThrowable(
529-
new Exception(
530-
'Cannot add MailCollector to Laravel Debugbar: ' . $e->getMessage(),
531-
$e->getCode(),
532-
$e
533-
)
534-
);
489+
} catch (Exception $e) {
490+
$this->addCollectorException('Cannot add SymfonyMailCollector', $e);
535491
}
536492
}
537493

538494
if ($this->shouldCollect('logs', false)) {
539495
try {
540496
$file = $this->app['config']->get('debugbar.options.logs.file');
541497
$this->addCollector(new LogsCollector($file));
542-
} catch (\Exception $e) {
543-
$this->addThrowable(
544-
new Exception(
545-
'Cannot add LogsCollector to Laravel Debugbar: ' . $e->getMessage(),
546-
$e->getCode(),
547-
$e
548-
)
549-
);
498+
} catch (Exception $e) {
499+
$this->addCollectorException('Cannot add LogsCollector', $e);
550500
}
551501
}
552502
if ($this->shouldCollect('files', false)) {
@@ -562,23 +512,17 @@ public function __toString(): string
562512
$this->app['config']->get('debugbar.options.auth.show_name')
563513
);
564514
$this->addCollector($authCollector);
565-
} catch (\Exception $e) {
566-
$this->addThrowable(
567-
new Exception(
568-
'Cannot add AuthCollector to Laravel Debugbar: ' . $e->getMessage(),
569-
$e->getCode(),
570-
$e
571-
)
572-
);
515+
} catch (Exception $e) {
516+
$this->addCollectorException('Cannot add AuthCollector', $e);
573517
}
574518
}
575519

576520
if ($this->shouldCollect('gate', false)) {
577521
try {
578522
$gateCollector = $this->app->make('Barryvdh\Debugbar\DataCollector\GateCollector');
579523
$this->addCollector($gateCollector);
580-
} catch (\Exception $e) {
581-
// No Gate collector
524+
} catch (Exception $e) {
525+
$this->addCollectorException('Cannot add GateCollector', $e);
582526
}
583527
}
584528

@@ -589,14 +533,8 @@ public function __toString(): string
589533
$cacheCollector = new CacheCollector($startTime, $collectValues);
590534
$this->addCollector($cacheCollector);
591535
$this->app['events']->subscribe($cacheCollector);
592-
} catch (\Exception $e) {
593-
$this->addThrowable(
594-
new Exception(
595-
'Cannot add CacheCollector to Laravel Debugbar: ' . $e->getMessage(),
596-
$e->getCode(),
597-
$e
598-
)
599-
);
536+
} catch (Exception $e) {
537+
$this->addCollectorException('Cannot add CacheCollector', $e);
600538
}
601539
}
602540

@@ -606,8 +544,8 @@ public function __toString(): string
606544
$this->app['events']->listen(\Illuminate\Queue\Events\JobQueued::class, function ($event) {
607545
$this['jobs']->countClass($event->job);
608546
});
609-
} catch (\Exception $e) {
610-
// No Jobs collector
547+
} catch (Exception $e) {
548+
$this->addCollectorException('Cannot add Jobs Collector', $e);
611549
}
612550
}
613551

@@ -696,7 +634,7 @@ public function stopMeasure($name)
696634
$collector = $this->getCollector('time');
697635
try {
698636
$collector->stopMeasure($name);
699-
} catch (\Exception $e) {
637+
} catch (Exception $e) {
700638
// $this->addThrowable($e);
701639
}
702640
}
@@ -727,6 +665,23 @@ public function addThrowable($e)
727665
}
728666
}
729667

668+
/**
669+
* Register collector exceptions
670+
*
671+
* @param string $message
672+
* @param Exception $exception
673+
*/
674+
protected function addCollectorException(string $message, Exception $exception)
675+
{
676+
$this->addThrowable(
677+
new Exception(
678+
$message . ' on Laravel Debugbar: ' . $exception->getMessage(),
679+
$exception->getCode(),
680+
$exception
681+
)
682+
);
683+
}
684+
730685
/**
731686
* Returns a JavascriptRenderer for this instance
732687
*
@@ -766,14 +721,8 @@ public function modifyResponse(Request $request, Response $response)
766721
$configCollector = new ConfigCollector();
767722
$configCollector->setData($app['config']->all());
768723
$this->addCollector($configCollector);
769-
} catch (\Exception $e) {
770-
$this->addThrowable(
771-
new Exception(
772-
'Cannot add ConfigCollector to Laravel Debugbar: ' . $e->getMessage(),
773-
$e->getCode(),
774-
$e
775-
)
776-
);
724+
} catch (Exception $e) {
725+
$this->addCollectorException('Cannot add ConfigCollector', $e);
777726
}
778727
}
779728

@@ -788,14 +737,8 @@ public function modifyResponse(Request $request, Response $response)
788737
if ($this->shouldCollect('session') && ! $this->hasCollector('session')) {
789738
try {
790739
$this->addCollector(new SessionCollector($sessionManager, $sessionHiddens));
791-
} catch (\Exception $e) {
792-
$this->addThrowable(
793-
new Exception(
794-
'Cannot add SessionCollector to Laravel Debugbar: ' . $e->getMessage(),
795-
$e->getCode(),
796-
$e
797-
)
798-
);
740+
} catch (Exception $e) {
741+
$this->addCollectorException('Cannot add SessionCollector', $e);
799742
}
800743
}
801744
} else {
@@ -810,28 +753,16 @@ public function modifyResponse(Request $request, Response $response)
810753
try {
811754
$reqId = $this->getCurrentRequestId();
812755
$this->addCollector(new RequestCollector($request, $response, $sessionManager, $reqId, $requestHiddens));
813-
} catch (\Exception $e) {
814-
$this->addThrowable(
815-
new Exception(
816-
'Cannot add SymfonyRequestCollector to Laravel Debugbar: ' . $e->getMessage(),
817-
$e->getCode(),
818-
$e
819-
)
820-
);
756+
} catch (Exception $e) {
757+
$this->addCollectorException('Cannot add SymfonyRequestCollector', $e);
821758
}
822759
}
823760

824761
if ($app['config']->get('debugbar.clockwork') && ! $this->hasCollector('clockwork')) {
825762
try {
826763
$this->addCollector(new ClockworkCollector($request, $response, $sessionManager, $requestHiddens));
827-
} catch (\Exception $e) {
828-
$this->addThrowable(
829-
new Exception(
830-
'Cannot add ClockworkCollector to Laravel Debugbar: ' . $e->getMessage(),
831-
$e->getCode(),
832-
$e
833-
)
834-
);
764+
} catch (Exception $e) {
765+
$this->addCollectorException('Cannot add ClockworkCollector', $e);
835766
}
836767

837768
$this->addClockworkHeaders($response);
@@ -840,7 +771,7 @@ public function modifyResponse(Request $request, Response $response)
840771
if ($response->isRedirection()) {
841772
try {
842773
$this->stackData();
843-
} catch (\Exception $e) {
774+
} catch (Exception $e) {
844775
$app['log']->error('Debugbar exception: ' . $e->getMessage());
845776
}
846777
} elseif (
@@ -853,7 +784,7 @@ public function modifyResponse(Request $request, Response $response)
853784
if ($app['config']->get('debugbar.add_ajax_timing', false)) {
854785
$this->addServerTimingHeaders($response);
855786
}
856-
} catch (\Exception $e) {
787+
} catch (Exception $e) {
857788
$app['log']->error('Debugbar exception: ' . $e->getMessage());
858789
}
859790
} elseif (
@@ -867,13 +798,13 @@ public function modifyResponse(Request $request, Response $response)
867798
try {
868799
// Just collect + store data, don't inject it.
869800
$this->collect();
870-
} catch (\Exception $e) {
801+
} catch (Exception $e) {
871802
$app['log']->error('Debugbar exception: ' . $e->getMessage());
872803
}
873804
} else {
874805
try {
875806
$this->injectDebugbar($response);
876-
} catch (\Exception $e) {
807+
} catch (Exception $e) {
877808
$app['log']->error('Debugbar exception: ' . $e->getMessage());
878809
}
879810
}

0 commit comments

Comments
 (0)