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
|
1 |
|
public function report(Exception $exception) |
46
|
|
|
{ |
47
|
1 |
|
parent::report($exception); |
48
|
1 |
|
} |
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
|
1 |
|
public function render($request, Exception $exception) |
59
|
|
|
{ |
60
|
|
|
// Validate 404 exceptions. |
61
|
1 |
|
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
|
1 |
|
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
|
1 |
|
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
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: