1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\LaravelTracy\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Http\Response; |
7
|
|
|
use Illuminate\Contracts\View\View; |
8
|
|
|
use Recca0120\LaravelTracy\Contracts\IHandler; |
9
|
|
|
use Recca0120\LaravelTracy\DebuggerManager; |
10
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
13
|
|
|
|
14
|
|
|
class LoggerHandler implements ExceptionHandler, IHandler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* app exception handler. |
18
|
|
|
* |
19
|
|
|
* @var \Illuminate\Contracts\Debug\ExceptionHandler |
20
|
|
|
*/ |
21
|
|
|
protected $exceptionHandler; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* $debuggerManager. |
25
|
|
|
* |
26
|
|
|
* @var \Recca0120\LaravelTracy\DebuggerManager |
27
|
|
|
*/ |
28
|
|
|
protected $debuggerManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* __construct. |
32
|
|
|
* |
33
|
|
|
* @param \Illuminate\Contracts\Debug\ExceptionHandler $exceptionHandler |
34
|
|
|
* @param \Recca0120\LaravelTracy\DebuggerManager $debuggerManager |
35
|
|
|
*/ |
36
|
1 |
|
public function __construct(ExceptionHandler $exceptionHandler, DebuggerManager $debuggerManager) |
37
|
|
|
{ |
38
|
1 |
|
$this->exceptionHandler = $exceptionHandler; |
39
|
1 |
|
$this->debuggerManager = $debuggerManager; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Report or log an exception. |
44
|
|
|
* |
45
|
|
|
* @param \Exception $e |
46
|
|
|
*/ |
47
|
|
|
public function report(Exception $e) |
48
|
|
|
{ |
49
|
|
|
$this->exceptionHandler->report($e); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* inject sending of email with error and render an exception into an HTTP response. |
54
|
|
|
* |
55
|
|
|
* @param \Illuminate\Http\Request $request |
56
|
|
|
* @param \Exception $e |
57
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
58
|
|
|
*/ |
59
|
|
|
public function render($request, Exception $e) |
60
|
|
|
{ |
61
|
|
|
$response = $this->exceptionHandler->render($request, $e); |
62
|
|
|
|
63
|
|
|
$this->debuggerManager->loggerExceptionHandler($e); |
64
|
|
|
|
65
|
|
|
return $response; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Render an exception to the console. |
70
|
|
|
* |
71
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
72
|
|
|
* @param \Exception $e |
73
|
|
|
*/ |
74
|
|
|
public function renderForConsole($output, Exception $e) |
75
|
|
|
{ |
76
|
|
|
$this->exceptionHandler->renderForConsole($output, $e); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* shouldRenderException. |
81
|
|
|
* |
82
|
|
|
* @param \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response $response |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
protected function shouldRenderException($response) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
if ($response instanceof RedirectResponse) { |
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($response instanceof JsonResponse) { |
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($response->getContent() instanceof View) { |
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($response instanceof Response && $response->getOriginalContent() instanceof View) { |
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.