1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoeSangue\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Auth\AuthenticationException; |
7
|
|
|
use Illuminate\Validation\ValidationException; |
8
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
9
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
10
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
11
|
|
|
|
12
|
|
|
class Handler extends ExceptionHandler |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* A list of the exception types that are not reported. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $dontReport = [ |
20
|
|
|
/*\Illuminate\Auth\AuthenticationException::class, |
|
|
|
|
21
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
22
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
23
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
24
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
25
|
|
|
\Illuminate\Validation\ValidationException::class, |
26
|
|
|
*/ |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* A list of the inputs that are never flashed for validation exceptions. |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $dontFlash = [ |
34
|
|
|
'password', |
35
|
|
|
'password_confirmation', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Report or log an exception. |
40
|
|
|
* |
41
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
42
|
|
|
* |
43
|
|
|
* @param \Exception $exception |
44
|
|
|
*/ |
45
|
|
|
public function report(Exception $exception) |
46
|
|
|
{ |
47
|
|
|
parent::report($exception); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Render an exception into an HTTP response. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Http\Request $request |
54
|
|
|
* @param \Exception $exception |
55
|
|
|
* |
56
|
|
|
* @return \Illuminate\Http\Response |
57
|
|
|
*/ |
58
|
|
|
public function render($request, Exception $exception) |
59
|
|
|
{ |
60
|
|
|
// Validate 404 exceptions. |
61
|
|
|
if ($exception instanceof NotFoundHttpException) { |
62
|
|
|
return response()->json( |
63
|
|
|
[ |
64
|
|
|
'error' => [ |
65
|
|
|
'description' => 'Invalid URI', |
66
|
|
|
'messages' => [ ] |
67
|
|
|
] |
68
|
|
|
], 404 |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Method not allowed exception handler |
73
|
|
|
if ($exception instanceof MethodNotAllowedHttpException) { |
74
|
|
|
return response()->json( |
75
|
|
|
[ |
76
|
|
|
'error' => [ |
77
|
|
|
'description' => 'Method Not Allowed', |
78
|
|
|
'messages' => [ ] |
79
|
|
|
] |
80
|
|
|
], 405 |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return parent::render($request, $exception); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Convert a validation exception into a JSON response. |
89
|
|
|
* |
90
|
|
|
* @param \Illuminate\Http\Request $request |
91
|
|
|
* @param \Illuminate\Validation\ValidationException $exception |
92
|
|
|
* @return \Illuminate\Http\JsonResponse |
93
|
|
|
*/ |
94
|
|
|
protected function invalidJson($request, ValidationException $exception) |
95
|
|
|
{ |
96
|
|
|
return response()->json($exception->errors(), $exception->status); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Convert an authentication exception into an unauthenticated response. |
101
|
|
|
* |
102
|
|
|
* @param \Illuminate\Http\Request $request |
103
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
104
|
|
|
* @return \Illuminate\Http\Response |
105
|
|
|
*/ |
106
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
107
|
|
|
{ |
108
|
|
|
if ($request->expectsJson()) { |
109
|
|
|
|
110
|
|
|
return response()->json([ 'error' => 'Unauthenticated.' ], 401); |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// return redirect()->guest('login'); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.