1
|
|
|
<?php namespace App\Exceptions; |
2
|
|
|
|
3
|
|
|
use App\Exceptions\Users\UserNotActivatedException; |
4
|
|
|
use Illuminate\Auth\AuthenticationException; |
5
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
6
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
7
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
8
|
|
|
use Illuminate\Http\Response as IlluminateResponse; |
9
|
|
|
use Illuminate\Session\TokenMismatchException; |
10
|
|
|
use Illuminate\Validation\ValidationException as IlluminateValidationException; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
12
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
13
|
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; |
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
|
|
|
AuthenticationException::class, |
24
|
|
|
AuthorizationException::class, |
25
|
|
|
HttpException::class, |
26
|
|
|
TokenMismatchException::class, |
27
|
|
|
IlluminateValidationException::class, |
28
|
|
|
ModelNotFoundException::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Report or log an exception. |
33
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
34
|
|
|
* |
35
|
|
|
* @param \Exception $e |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
5 |
|
public function report(\Exception $e) |
40
|
|
|
{ |
41
|
5 |
|
parent::report($e); |
42
|
5 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Render an exception into an HTTP response. |
46
|
|
|
* |
47
|
|
|
* @param \Illuminate\Http\Request $request |
48
|
|
|
* @param \Exception $e |
49
|
|
|
* |
50
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response |
51
|
|
|
*/ |
52
|
4 |
|
public function render($request, \Exception $e) |
53
|
|
|
{ |
54
|
4 |
|
if ($e instanceof ModelNotFoundException) { |
55
|
2 |
|
$e = new NotFoundHttpException($e->getMessage(), $e); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
if ($request->expectsJson()) { |
59
|
4 |
|
$exceptionClass = get_class($e); |
60
|
4 |
|
$responseBody = ['exception' => $exceptionClass, 'message' => $e->getMessage()]; |
61
|
|
|
|
62
|
|
|
# FileStream exceptions response body |
63
|
4 |
|
$preventRetry = true; |
64
|
4 |
|
$resetUpload = false; |
65
|
4 |
|
if ($e instanceof FileStream\UploadFilenameIsEmptyException) { |
66
|
|
|
$preventRetry = false; |
67
|
4 |
|
} elseif ($e instanceof FileStream\UploadIncompleteException) { |
68
|
|
|
$preventRetry = false; |
69
|
|
|
$resetUpload = true; |
70
|
4 |
|
} elseif ($e instanceof FileStream\UploadAttemptFailedException) { |
71
|
|
|
$preventRetry = false; |
72
|
|
|
} |
73
|
4 |
|
if (strpos($exceptionClass, 'App\Exceptions\FileStream') !== false) { |
74
|
|
|
$responseBody = ['exception' => $exceptionClass, 'error' => $e->getMessage(), 'preventRetry' => $preventRetry, 'reset' => $resetUpload]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
# Status codes |
78
|
4 |
|
if ($e instanceof UnauthorizedHttpException) { |
79
|
|
|
$statusCode = IlluminateResponse::HTTP_UNAUTHORIZED; |
80
|
4 |
|
} elseif ($e instanceof UserNotActivatedException) { |
81
|
|
|
$statusCode = IlluminateResponse::HTTP_FORBIDDEN; |
82
|
4 |
|
} elseif ($e instanceof NotFoundHttpException) { |
83
|
2 |
|
$statusCode = IlluminateResponse::HTTP_NOT_FOUND; |
84
|
4 |
|
} elseif ($e instanceof \BadMethodCallException) { |
85
|
|
|
$statusCode = IlluminateResponse::HTTP_METHOD_NOT_ALLOWED; |
86
|
4 |
|
} elseif ($e instanceof \UnexpectedValueException || $e instanceof IlluminateValidationException || $e instanceof TokenMismatchException) { |
87
|
3 |
|
$statusCode = IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY; |
88
|
3 |
|
if ($e instanceof IlluminateValidationException) { |
89
|
|
|
/** @var \Illuminate\Support\MessageBag $messageBag */ |
90
|
3 |
|
$messageBag = $e->validator->getMessageBag(); |
91
|
3 |
|
$responseBody['message'] = $messageBag->getMessages(); |
92
|
|
|
} |
93
|
1 |
|
} elseif ($e instanceof \OverflowException) { |
94
|
|
|
$statusCode = IlluminateResponse::HTTP_REQUEST_ENTITY_TOO_LARGE; |
95
|
|
|
} else { |
96
|
1 |
|
$statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : $e->getCode(); |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
if (empty($statusCode)) { |
100
|
|
|
$statusCode = IlluminateResponse::HTTP_INTERNAL_SERVER_ERROR; |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
return response()->json($responseBody)->setStatusCode($statusCode); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($request->method() != 'GET' && $request->header('content-type') == 'application/x-www-form-urlencoded') { |
107
|
|
|
return redirect()->back()->withInput($request->all())->withErrors($e->getMessage()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return parent::render($request, $e); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Convert an authentication exception into an unauthenticated response. |
115
|
|
|
* |
116
|
|
|
* @param \Illuminate\Http\Request $request |
117
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
118
|
|
|
* |
119
|
|
|
* @return \Illuminate\Http\Response |
120
|
|
|
*/ |
121
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
if ($request->expectsJson()) { |
124
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return redirect()->guest('login'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.