-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExceptionHandler.php
99 lines (80 loc) · 3.2 KB
/
ExceptionHandler.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
<?php
declare(strict_types=1);
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
use Sentry\Laravel\Integration;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
class ExceptionHandler extends Handler
{
protected $dontFlash = [
"current_password",
"password",
];
public function register(): void
{
$this->reportable(function (Throwable $exception): void {
Integration::captureUnhandledException($exception);
});
}
/**
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
*/
public function render($request, Throwable $exception)
{
if ($exception instanceof ValidationException) {
return back()->withErrors($exception->errors());
}
$response = parent::render($request, $exception);
$statusCode = $response->status();
app()->setLocale("en");
$encryptedCookie = $request->cookie("locale");
if ($encryptedCookie) {
if (in_array($encryptedCookie, ["pl", "en"], true)) {
app()->setLocale($encryptedCookie);
} else {
$decryptedCookie = Crypt::decryptString($encryptedCookie);
$languageCode = substr($decryptedCookie, strpos($decryptedCookie, "|") + 1);
if (in_array($languageCode, ["pl", "en"], true)) {
app()->setLocale($languageCode);
}
}
}
switch ($statusCode) {
case Response::HTTP_INTERNAL_SERVER_ERROR:
case Response::HTTP_SERVICE_UNAVAILABLE:
case Response::HTTP_TOO_MANY_REQUESTS:
$statusTitle = __($response->statusText());
$statusDescription = $this->getDescriptionByStatusCode($statusCode);
break;
case 419:
$statusTitle = __("Session Expired");
$statusDescription = __("Your session has expired. Please log in and try again.");
break;
default:
$statusTitle = __("Not Found");
$statusDescription = __("Sorry, the page you were looking for could not be found.");
$statusCode = Response::HTTP_NOT_FOUND;
break;
}
return Inertia::render("Error", [
"statusTitle" => $statusTitle,
"statusDescription" => $statusDescription,
"statusCode" => $statusCode,
])
->toResponse($request)
->setStatusCode($statusCode);
}
protected function getDescriptionByStatusCode(int $statusCode): string
{
$descriptions = [
Response::HTTP_INTERNAL_SERVER_ERROR => __("Oops! Something went wrong on our end. Please try again later."),
Response::HTTP_SERVICE_UNAVAILABLE => __("Oops! The service is currently unavailable. Please try again later."),
Response::HTTP_TOO_MANY_REQUESTS => __("Oops! Too many requests. Please try again later."),
];
return $descriptions[$statusCode] ?? __("Oops. Something went wrong. Try again later.");
}
}