1
|
|
|
<?php namespace App\Exceptions; |
2
|
|
|
|
3
|
|
|
use App\Exceptions\Users\UserNotActivatedException; |
4
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
5
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
6
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
7
|
|
|
use Illuminate\Http\Response as IlluminateResponse; |
8
|
|
|
use Illuminate\Session\TokenMismatchException; |
9
|
|
|
use Illuminate\Validation\ValidationException as IlluminateValidationException; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
12
|
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; |
13
|
|
|
|
14
|
|
|
class Handler extends ExceptionHandler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* A list of the exception types that should not be reported. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $dontReport = [ |
22
|
|
|
AuthorizationException::class, |
23
|
|
|
HttpException::class, |
24
|
|
|
IlluminateValidationException::class, |
25
|
|
|
ModelNotFoundException::class, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Report or log an exception. |
30
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
31
|
|
|
* |
32
|
|
|
* @param \Exception $e |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
12 |
|
public function report(\Exception $e) |
37
|
|
|
{ |
38
|
12 |
|
parent::report($e); |
39
|
12 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Render an exception into an HTTP response. |
43
|
|
|
* |
44
|
|
|
* @param \Illuminate\Http\Request $request |
45
|
|
|
* @param \Exception $e |
46
|
|
|
* |
47
|
|
|
* @return \Illuminate\Http\Response |
48
|
|
|
* |
49
|
|
|
* @todo Separate API exception handling from Web one (possibly separating Laravel and Lumen apps). |
50
|
|
|
*/ |
51
|
12 |
|
public function render($request, \Exception $e) |
52
|
|
|
{ |
53
|
12 |
|
if ($e instanceof ModelNotFoundException) { |
54
|
5 |
|
$e = new NotFoundHttpException($e->getMessage(), $e); |
55
|
5 |
|
} |
56
|
|
|
|
57
|
12 |
|
if ($request->ajax() || $request->wantsJson()) { |
58
|
12 |
|
$exceptionClass = get_class($e); |
59
|
12 |
|
$responseBody = ['exception' => $exceptionClass, 'message' => $e->getMessage()]; |
60
|
|
|
|
61
|
|
|
# FileStream exceptions response body |
62
|
12 |
|
$preventRetry = true; |
63
|
12 |
|
$resetUpload = false; |
64
|
12 |
|
if ($e instanceof FileStream\UploadFilenameIsEmptyException) { |
65
|
|
|
$preventRetry = false; |
66
|
12 |
|
} elseif ($e instanceof FileStream\UploadIncompleteException) { |
67
|
|
|
$preventRetry = false; |
68
|
|
|
$resetUpload = true; |
69
|
12 |
|
} elseif ($e instanceof FileStream\UploadAttemptFailedException) { |
70
|
|
|
$preventRetry = false; |
71
|
|
|
} |
72
|
12 |
|
if (strpos($exceptionClass, 'App\Exceptions\FileStream') !== false) { |
73
|
|
|
$responseBody = ['exception' => $exceptionClass, 'error' => $e->getMessage(), 'preventRetry' => $preventRetry, 'reset' => $resetUpload]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
# Status codes |
77
|
12 |
|
if ($e instanceof UnauthorizedHttpException) { |
78
|
1 |
|
$statusCode = IlluminateResponse::HTTP_UNAUTHORIZED; |
79
|
12 |
|
} elseif ($e instanceof UserNotActivatedException) { |
80
|
|
|
$statusCode = IlluminateResponse::HTTP_FORBIDDEN; |
81
|
12 |
|
} elseif ($e instanceof NotFoundHttpException) { |
82
|
5 |
|
$statusCode = IlluminateResponse::HTTP_NOT_FOUND; |
83
|
12 |
|
} elseif ($e instanceof \BadMethodCallException) { |
84
|
2 |
|
$statusCode = IlluminateResponse::HTTP_METHOD_NOT_ALLOWED; |
85
|
11 |
|
} elseif ($e instanceof \UnexpectedValueException || $e instanceof IlluminateValidationException || $e instanceof TokenMismatchException) { |
86
|
8 |
|
$statusCode = IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY; |
87
|
8 |
|
if ($e instanceof IlluminateValidationException) { |
88
|
6 |
|
$messageBag = $e->validator->errors(); |
89
|
6 |
|
$responseBody['message'] = $messageBag->getMessages(); |
90
|
6 |
|
} |
91
|
9 |
|
} elseif ($e instanceof \OverflowException) { |
92
|
|
|
$statusCode = IlluminateResponse::HTTP_REQUEST_ENTITY_TOO_LARGE; |
93
|
|
|
} else { |
94
|
1 |
|
$statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : $e->getCode(); |
95
|
|
|
} |
96
|
|
|
|
97
|
12 |
|
if (empty($statusCode)) { |
98
|
1 |
|
$statusCode = IlluminateResponse::HTTP_INTERNAL_SERVER_ERROR; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
12 |
|
return response()->json($responseBody)->setStatusCode($statusCode); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($request->method() != 'GET' && $request->header('content-type') == 'application/x-www-form-urlencoded') { |
105
|
|
|
return redirect()->back()->withInput($request->all())->withErrors($e->getMessage()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return parent::render($request, $e); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|