-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHandler.php
121 lines (106 loc) · 3.15 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
<?php
namespace App\Exceptions;
use Exception;
use Flugg\Responder\Exceptions\ConvertsExceptions;
use Flugg\Responder\Exceptions\Http\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\App;
use Inertia\Inertia;
use Symfony\Component\HttpFoundation\Response;
class Handler extends ExceptionHandler
{
use ConvertsExceptions;
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
];
/**
* 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
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
*
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
// Handles API exceptions first.
if ($response = $this->isApiException($request, $exception)) {
return $response;
}
// Handles Inertia exceptions second.
return $this->handleInertiaException($request, $exception);
}
/**
* Renders an exception into an API response.
*
* @param \Illuminate\Http\Request $request
* @param Exception $exception
*
* @return Illuminate\Http\JsonResponse|null
*/
private function isApiException($request, Exception $exception)
{
if ($request->expectsJson() || $exception instanceof HttpException) {
$this->convertDefaultException($exception);
return $this->renderResponse($exception);
}
return null;
}
/**
* Render an exception into an Inertia response, as long
* as the application is not in a local environment.
*
* @param \Illuminate\Http\Request $request
* @param Exception $exception
*
* @return \Illuminate\Contracts\Support\Responsable
*/
private function handleInertiaException($request, Exception $exception)
{
$response = parent::render($request, $exception);
if ($this->shouldRenderInertiaError($response->status())) {
return Inertia::render('Security/Error', ['code' => $response->status()])
->toResponse($request)
->setStatusCode($response->status());
}
return $response;
}
/**
* Checks if the given HTTP status should render an error page.
*
* @param int $status
*
* @return bool
*/
private function shouldRenderInertiaError(int $status)
{
return !App::environment('local') && in_array($status, [
Response::HTTP_INTERNAL_SERVER_ERROR,
Response::HTTP_SERVICE_UNAVAILABLE,
Response::HTTP_NOT_FOUND,
Response::HTTP_FORBIDDEN,
]);
}
}