1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Exceptions; |
4
|
|
|
|
5
|
|
|
use App\Mail\ExceptionOccured; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Auth\AuthenticationException; |
8
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
9
|
|
|
use Illuminate\Support\Facades\Log; |
10
|
|
|
use Mail; |
11
|
|
|
use Response; |
12
|
|
|
use Symfony\Component\Debug\Exception\FlattenException; |
13
|
|
|
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler; |
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
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
24
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
25
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
26
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
27
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
28
|
|
|
\Illuminate\Validation\ValidationException::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
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function report(Exception $exception) |
41
|
|
|
{ |
42
|
|
|
$enableEmailExceptions = config('exceptions.emailExceptionEnabled'); |
43
|
|
|
|
44
|
|
|
if ($enableEmailExceptions === '') { |
45
|
|
|
$enableEmailExceptions = config('exceptions.emailExceptionEnabledDefault'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($enableEmailExceptions && $this->shouldReport($exception)) { |
49
|
|
|
$this->sendEmail($exception); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
parent::report($exception); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Render an exception into an HTTP response. |
57
|
|
|
* |
58
|
|
|
* @param \Illuminate\Http\Request $request |
59
|
|
|
* @param \Exception $exception |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Http\Response |
62
|
|
|
*/ |
63
|
|
|
public function render($request, Exception $exception) |
64
|
|
|
{ |
65
|
|
|
$userLevelCheck = $exception instanceof \jeremykenedy\LaravelRoles\Exceptions\RoleDeniedException || |
66
|
|
|
$exception instanceof \jeremykenedy\LaravelRoles\Exceptions\RoleDeniedException || |
67
|
|
|
$exception instanceof \jeremykenedy\LaravelRoles\Exceptions\PermissionDeniedException || |
68
|
|
|
$exception instanceof \jeremykenedy\LaravelRoles\Exceptions\LevelDeniedException; |
69
|
|
|
|
70
|
|
|
if ($userLevelCheck) { |
71
|
|
|
if ($request->expectsJson()) { |
72
|
|
|
return Response::json([ |
|
|
|
|
73
|
|
|
'error' => 403, |
74
|
|
|
'message' => 'Unauthorized.', |
75
|
|
|
], 403); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
abort(403); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return parent::render($request, $exception); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Convert an authentication exception into an unauthenticated response. |
86
|
|
|
* |
87
|
|
|
* @param \Illuminate\Http\Request $request |
88
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
89
|
|
|
* |
90
|
|
|
* @return \Illuminate\Http\Response |
91
|
|
|
*/ |
92
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
93
|
|
|
{ |
94
|
|
|
if ($request->expectsJson()) { |
95
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return redirect()->guest(route('login')); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Sends an email upon exception. |
103
|
|
|
* |
104
|
|
|
* @param \Exception $exception |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function sendEmail(Exception $exception) |
109
|
|
|
{ |
110
|
|
|
try { |
111
|
|
|
$e = FlattenException::create($exception); |
112
|
|
|
$handler = new SymfonyExceptionHandler(); |
|
|
|
|
113
|
|
|
$html = $handler->getHtml($e); |
114
|
|
|
|
115
|
|
|
Mail::send(new ExceptionOccured($html)); |
116
|
|
|
} catch (Exception $exception) { |
117
|
|
|
Log::error($exception); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|