1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GiveBlood\Units; |
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 Handler; |
11
|
|
|
|
12
|
|
|
class ExceptionHandler extends Handler |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* A list of the exception types that are not reported. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $dontReport = [ |
20
|
|
|
// |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A list of the inputs that are never flashed for validation exceptions. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $dontFlash = [ |
29
|
|
|
'password', |
30
|
|
|
'password_confirmation', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Report or log an exception. |
35
|
|
|
* |
36
|
|
|
* @param \Exception $exception |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function report(Exception $exception) |
40
|
|
|
{ |
41
|
|
|
parent::report($exception); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Render an exception into an HTTP response. |
46
|
|
|
* |
47
|
|
|
* @param \Illuminate\Http\Request $request |
48
|
|
|
* @param \Exception $exception |
49
|
|
|
* @return \Illuminate\Http\Response |
50
|
|
|
*/ |
51
|
|
|
public function render($request, Exception $exception) |
52
|
|
|
{ |
53
|
|
|
// Validate 404 exceptions. |
54
|
|
|
if ($exception instanceof NotFoundHttpException) { |
55
|
|
|
return response()->json( |
|
|
|
|
56
|
|
|
[ |
57
|
|
|
'error' => [ |
58
|
|
|
'description' => 'Invalid URI', |
59
|
|
|
'messages' => [ ] |
60
|
|
|
] |
61
|
|
|
], 404 |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Method not allowed exception handler |
66
|
|
|
if ($exception instanceof MethodNotAllowedHttpException) { |
67
|
|
|
return response()->json( |
68
|
|
|
[ |
69
|
|
|
'error' => [ |
70
|
|
|
'description' => 'Method Not Allowed', |
71
|
|
|
'messages' => [ ] |
72
|
|
|
] |
73
|
|
|
], 405 |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
return parent::render($request, $exception); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Convert a validation exception into a JSON response. |
81
|
|
|
* |
82
|
|
|
* @param \Illuminate\Http\Request $request |
83
|
|
|
* @param \Illuminate\Validation\ValidationException $exception |
84
|
|
|
* @return \Illuminate\Http\JsonResponse |
85
|
|
|
*/ |
86
|
|
|
protected function invalidJson($request, ValidationException $exception) |
87
|
|
|
{ |
88
|
|
|
return response()->json($exception->errors(), $exception->status); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Convert an authentication exception into an unauthenticated response. |
93
|
|
|
* |
94
|
|
|
* @param \Illuminate\Http\Request $request |
95
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
96
|
|
|
* @return \Illuminate\Http\Response |
97
|
|
|
*/ |
98
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
99
|
|
|
{ |
100
|
|
|
if ($request->expectsJson()) { |
101
|
|
|
|
102
|
|
|
return response()->json([ 'error' => 'Unauthenticated.' ], 401); |
|
|
|
|
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// return redirect()->guest('login'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
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: