-
Notifications
You must be signed in to change notification settings - Fork 461
/
Copy pathHandler.php
executable file
·163 lines (142 loc) · 4.97 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
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
/**
* Class Handler.
*/
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
// GeneralException::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.
*
* @param Exception $exception
* @return mixed|void
*
* @throws Exception
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Exception
*/
public function render($request, Exception $exception)
{
if (strpos($request->url(), '/api/') !== false) {
Log::debug('API Request Exception - '.$request->url().' - '.$exception->getMessage().(! empty($request->all()) ? ' - '.json_encode($request->except(['password'])) : ''));
if ($exception instanceof AuthorizationException) {
return $this->setStatusCode(Response::HTTP_FORBIDDEN)->respondWithError($exception->getMessage());
}
if ($exception instanceof MethodNotAllowedHttpException) {
return $this->setStatusCode(Response::HTTP_METHOD_NOT_ALLOWED)->respondWithError('Please check HTTP Request Method. - MethodNotAllowedHttpException');
}
if ($exception instanceof NotFoundHttpException) {
return $this->setStatusCode(Response::HTTP_NOT_FOUND)->respondWithError('Please check your URL to make sure request is formatted properly. - NotFoundHttpException');
}
if ($exception instanceof GeneralException) {
return $this->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR)->respondWithError($exception->getMessage());
}
if ($exception instanceof ModelNotFoundException) {
return $this->setStatusCode(Response::HTTP_NOT_FOUND)->respondWithError('Item could not be found. Please check identifier.');
}
if ($exception instanceof AuthenticationException) {
return $this->setStatusCode(Response::HTTP_UNAUTHORIZED)->respondWithError('Unauthenticated.');
}
if ($exception instanceof ValidationException) {
Log::debug('API Validation Exception - '.json_encode($exception->validator->messages()));
return $this->setStatusCode(422)->respondWithError($exception->validator->messages());
}
/*
* Redirect if token mismatch error
* Usually because user stayed on the same screen too long and their session expired
*/
if ($exception instanceof UnauthorizedHttpException) {
switch (get_class($exception->getPrevious())) {
case self::class:
return $this->setStatusCode($exception->getStatusCode())->respondWithError('Token has not been provided.');
}
}
}
return parent::render($request, $exception);
}
/**
* get the status code.
*
* @return statuscode
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* set the status code.
*
* @param [type] $statusCode [description]
* @return statuscode
*/
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
/**
* respond with error.
*
* @param $message
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithError($message)
{
return $this->respond([
'error' => [
'message' => $message,
'status_code' => $this->getStatusCode(),
],
]);
}
/**
* Respond.
*
* @param array $data
* @param array $headers
* @return \Illuminate\Http\JsonResponse
*/
public function respond($data, $headers = [])
{
return response()->json($data, $this->getStatusCode(), $headers);
}
}