1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Exceptions; |
4
|
|
|
|
5
|
|
|
use CloudCreativity\JsonApi\Document\Error; |
6
|
|
|
use CloudCreativity\LaravelJsonApi\Exceptions\HandlesErrors; |
7
|
|
|
use Exception; |
8
|
|
|
use Illuminate\Auth\AuthenticationException; |
9
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
10
|
|
|
use Neomerx\JsonApi\Exceptions\JsonApiException; |
11
|
|
|
|
12
|
|
|
class Handler extends ExceptionHandler |
13
|
|
|
{ |
14
|
|
|
use HandlesErrors; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A list of the exception types that should not be reported. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $dontReport = [ |
22
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
23
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
24
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
25
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
26
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
27
|
|
|
\Illuminate\Validation\ValidationException::class, |
28
|
|
|
JsonApiException::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Report or log an exception. |
33
|
|
|
* |
34
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
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
|
|
|
if ($this->isJsonApi()) { |
54
|
|
|
return $this->renderJsonApi($request, $exception); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return parent::render($request, $exception); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Convert an authentication exception into an unauthenticated response. |
62
|
|
|
* |
63
|
|
|
* @param \Illuminate\Http\Request $request |
64
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
65
|
|
|
* @return \Illuminate\Http\Response |
66
|
|
|
*/ |
67
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if ($request->expectsJson()) { |
70
|
|
|
return response()->json(['errors' => [ |
71
|
|
|
[Error::STATUS => 401, Error::TITLE => 'Authentication error', Error::DETAIL => 'Unauthenticated']] |
72
|
|
|
], 401); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return redirect()->guest('login'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.