1
|
|
|
<?php namespace App\Exceptions; |
2
|
|
|
|
3
|
|
|
use Cartalyst\Sentinel\Checkpoints\NotActivatedException; |
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
|
12 |
|
public function render($request, \Exception $e) |
50
|
|
|
{ |
51
|
12 |
|
if ($e instanceof ModelNotFoundException) { |
52
|
5 |
|
$e = new NotFoundHttpException($e->getMessage(), $e); |
53
|
5 |
|
} |
54
|
|
|
|
55
|
12 |
|
if ($request->ajax() || $request->wantsJson()) { |
56
|
12 |
|
$exceptionClass = get_class($e); |
57
|
12 |
|
$responseBody = ['exception' => $exceptionClass, 'message' => $e->getMessage()]; |
58
|
|
|
|
59
|
|
|
# FileStream exceptions response body |
60
|
12 |
|
$preventRetry = true; |
61
|
12 |
|
$resetUpload = false; |
62
|
12 |
|
if ($e instanceof FileStream\UploadFilenameIsEmptyException) { |
63
|
|
|
$preventRetry = false; |
64
|
12 |
|
} elseif ($e instanceof FileStream\UploadIncompleteException) { |
65
|
|
|
$preventRetry = false; |
66
|
|
|
$resetUpload = true; |
67
|
12 |
|
} elseif ($e instanceof FileStream\UploadAttemptFailedException) { |
68
|
|
|
$preventRetry = false; |
69
|
|
|
} |
70
|
12 |
|
if (strpos($exceptionClass, 'App\Exceptions\FileStream') !== false) { |
71
|
|
|
$responseBody = ['exception' => $exceptionClass, 'error' => $e->getMessage(), 'preventRetry' => $preventRetry, 'reset' => $resetUpload]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
# Status codes |
75
|
12 |
|
if ($e instanceof UnauthorizedHttpException) { |
76
|
1 |
|
$statusCode = IlluminateResponse::HTTP_UNAUTHORIZED; |
77
|
12 |
|
} elseif ($e instanceof NotActivatedException) { |
78
|
|
|
$statusCode = IlluminateResponse::HTTP_FORBIDDEN; |
79
|
11 |
|
} elseif ($e instanceof NotFoundHttpException) { |
80
|
5 |
|
$statusCode = IlluminateResponse::HTTP_NOT_FOUND; |
81
|
11 |
|
} elseif ($e instanceof \BadMethodCallException) { |
82
|
2 |
|
$statusCode = IlluminateResponse::HTTP_METHOD_NOT_ALLOWED; |
83
|
10 |
|
} elseif ($e instanceof \UnexpectedValueException || $e instanceof IlluminateValidationException || $e instanceof TokenMismatchException) { |
84
|
8 |
|
$statusCode = IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY; |
85
|
8 |
|
} elseif ($e instanceof \OverflowException) { |
86
|
|
|
$statusCode = IlluminateResponse::HTTP_REQUEST_ENTITY_TOO_LARGE; |
87
|
|
|
} else { |
88
|
|
|
$statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : $e->getCode(); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
12 |
|
if (empty($statusCode)) { |
92
|
|
|
$statusCode = IlluminateResponse::HTTP_INTERNAL_SERVER_ERROR; |
93
|
|
|
} |
94
|
|
|
|
95
|
12 |
|
return response()->json($responseBody)->setStatusCode($statusCode); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($request->method() != 'GET' && $request->header('content-type') == 'application/x-www-form-urlencoded') { |
99
|
|
|
return redirect()->back()->withInput($request->all())->withErrors($e->getMessage()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return parent::render($request, $e); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|