1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoeSangue\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Auth\AuthenticationException; |
7
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
8
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
9
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
10
|
|
|
|
11
|
|
|
class Handler extends ExceptionHandler |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* A list of the exception types that should not be reported. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $dontReport = [ |
19
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
20
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
21
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
22
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
23
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
24
|
|
|
\Illuminate\Validation\ValidationException::class, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Report or log an exception. |
29
|
|
|
* |
30
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
31
|
|
|
* |
32
|
|
|
* @param \Exception $exception |
33
|
|
|
*/ |
34
|
|
|
public function report(Exception $exception) |
35
|
|
|
{ |
36
|
|
|
parent::report($exception); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Render an exception into an HTTP response. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Http\Request $request |
43
|
|
|
* @param \Exception $exception |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Http\Response |
46
|
|
|
*/ |
47
|
|
|
public function render($request, Exception $exception) |
48
|
|
|
{ |
49
|
|
|
// Validate 404 exceptions. |
50
|
|
|
if($exception instanceof NotFoundHttpException) { |
51
|
|
|
return response()->json( |
52
|
|
|
[ |
53
|
|
|
'error' => [ |
54
|
|
|
'description' => 'Invalid URI', |
55
|
|
|
'messages' => [] |
56
|
|
|
] |
57
|
|
|
], 404 |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Method not allowed exception handler |
62
|
|
|
if($exception instanceof MethodNotAllowedHttpException) { |
63
|
|
|
return response()->json( |
64
|
|
|
[ |
65
|
|
|
'error' => [ |
66
|
|
|
'description' => 'Method Not Allowed', |
67
|
|
|
'messages' => [] |
68
|
|
|
] |
69
|
|
|
], 405 |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return parent::render($request, $exception); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Convert an authentication exception into an unauthenticated response. |
78
|
|
|
* |
79
|
|
|
* @param \Illuminate\Http\Request $request |
80
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
81
|
|
|
* |
82
|
|
|
* @return \Illuminate\Http\Response |
83
|
|
|
*/ |
84
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
if ($request->expectsJson()) { |
87
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return redirect()->guest('login'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.