1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Validation\ValidationException; |
7
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
8
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
9
|
|
|
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
13
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
14
|
|
|
|
15
|
|
|
class Handler extends ExceptionHandler |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* A list of the exception types that should not be reported. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $dontReport = [ |
23
|
|
|
AuthorizationException::class, |
24
|
|
|
HttpException::class, |
25
|
|
|
ModelNotFoundException::class, |
26
|
|
|
ValidationException::class, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Report or log an exception. |
31
|
|
|
* |
32
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
33
|
|
|
* |
34
|
|
|
* @param \Exception $e |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function report(Exception $e) |
38
|
|
|
{ |
39
|
|
|
parent::report($e); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Render an exception into an HTTP response. |
44
|
|
|
* |
45
|
|
|
* @param \Illuminate\Http\Request $request |
46
|
|
|
* @param \Exception $e |
47
|
|
|
* @return \Illuminate\Http\Response |
48
|
|
|
*/ |
49
|
|
|
public function render($request, Exception $e) |
50
|
|
|
{ |
51
|
|
|
if(env('APP_DEBUG')){ |
52
|
|
|
return parent::render($request, $e); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if($e instanceof NotFoundHttpException){ |
56
|
|
|
return response()->json(['message' => 'Bad Request', 'code' => 400], 400); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if($e instanceof MethodNotAllowedHttpException){ |
60
|
|
|
return response()->json(['message' => 'Not Found', 'code' => 404], 404); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return response()->json(['message' => 'Unexpected Error', 'code' => 500], 500); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
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: