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