This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
/
Handler.php
173 lines (150 loc) 路 5.08 KB
/
Handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace Barryvanveen\Exceptions;
use Bugsnag;
use Exception;
use GoogleTagManager;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Validation\ValidationException;
use Input;
use Meta;
use Redirect;
use Response;
use Session;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use View;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
*/
public function report(Exception $exception)
{
if ($this->shouldReport($exception)) {
Bugsnag::notifyException($exception);
}
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
*
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function render($request, Exception $exception)
{
$exception = $this->prepareException($exception);
if ($exception instanceof TokenMismatchException) {
return $this->tokenMismatchResponse();
} elseif ($exception instanceof InvalidLoginException) {
return $this->invalidLoginResponse();
} elseif ($exception instanceof HttpResponseException) {
return $exception->getResponse();
} elseif ($exception instanceof AuthenticationException) {
return $this->unauthenticated($request, $exception);
} elseif ($exception instanceof ValidationException) {
return $this->convertValidationExceptionToResponse($exception, $request);
}
if (config('app.debug') && ! ($exception instanceof ValidationException) && ! ($exception instanceof HttpResponseException)) {
return $this->renderWhoopsResponse($exception);
}
if ($exception instanceof NotFoundHttpException) {
return $this->renderErrorPageResponse(404);
}
if ($exception instanceof MethodNotAllowedHttpException) {
return $this->renderErrorPageResponse(403);
}
return $this->renderErrorPageResponse(500);
}
/**
* @param Exception $exception
*
* @return \Illuminate\Http\Response
*/
protected function renderWhoopsResponse(Exception $exception)
{
return $this->toIlluminateResponse($this->convertExceptionToResponse($exception), $exception);
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
protected function tokenMismatchResponse()
{
Session::regenerateToken();
$errors = [
'_token' => [
trans('validation.token-mismatch'),
],
];
return Redirect::back()->withInput(Input::except('_token'))->withErrors($errors);
}
/**
* Display.
*
* @param $status_code
*
* @return \Illuminate\Http\Response
*/
protected function renderErrorPageResponse($status_code)
{
Meta::set('title', trans('meta.pagetitle-'.$status_code).' - '.trans('meta.pagetitle-default'));
GoogleTagManager::set('errorcode', $status_code);
return Response::make(View::make('errors.'.$status_code), $status_code);
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function invalidLoginResponse()
{
$errors = [
'password' => [
trans('validation.invalid-login'),
],
];
return Redirect::back()->withInput(['email' => Input::get('email')])->withErrors($errors);
}
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
*
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest(route('admin.login'));
}
}