improved exception handler
This commit is contained in:
parent
3883c0ee55
commit
9d7cd71e09
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Exceptions;
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Akaunting\Money\Exceptions\UnexpectedAmountException;
|
||||||
use App\Exceptions\Http\Resource as ResourceException;
|
use App\Exceptions\Http\Resource as ResourceException;
|
||||||
use Illuminate\Auth\AuthenticationException;
|
use Illuminate\Auth\AuthenticationException;
|
||||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
@ -10,6 +11,7 @@ use Illuminate\Http\Exceptions\ThrottleRequestsException;
|
|||||||
use Illuminate\Http\Response;
|
use Illuminate\Http\Response;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use Illuminate\View\ViewException;
|
||||||
use Symfony\Component\Debug\Exception\FatalThrowableError;
|
use Symfony\Component\Debug\Exception\FatalThrowableError;
|
||||||
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
@ -17,21 +19,31 @@ use Throwable;
|
|||||||
|
|
||||||
class Handler extends ExceptionHandler
|
class Handler extends ExceptionHandler
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* A list of exception types with their corresponding custom log levels.
|
||||||
|
*
|
||||||
|
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
|
||||||
|
*/
|
||||||
|
protected $levels = [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A list of the exception types that are not reported.
|
* A list of the exception types that are not reported.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array<int, class-string<\Throwable>>
|
||||||
*/
|
*/
|
||||||
protected $dontReport = [
|
protected $dontReport = [
|
||||||
//
|
//
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A list of the inputs that are never flashed for validation exceptions.
|
* A list of the inputs that are never flashed to the session on validation exceptions.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array<int, string>
|
||||||
*/
|
*/
|
||||||
protected $dontFlash = [
|
protected $dontFlash = [
|
||||||
|
'current_password',
|
||||||
'password',
|
'password',
|
||||||
'password_confirmation',
|
'password_confirmation',
|
||||||
];
|
];
|
||||||
@ -95,7 +107,9 @@ class Handler extends ExceptionHandler
|
|||||||
if ($exception instanceof NotFoundHttpException) {
|
if ($exception instanceof NotFoundHttpException) {
|
||||||
// ajax 404 json feedback
|
// ajax 404 json feedback
|
||||||
if ($request->ajax()) {
|
if ($request->ajax()) {
|
||||||
return response()->json(['error' => 'Not Found'], 404);
|
return response()->json([
|
||||||
|
'error' => trans('errors.header.404'),
|
||||||
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
flash(trans('errors.body.page_not_found'))->error()->important();
|
flash(trans('errors.body.page_not_found'))->error()->important();
|
||||||
@ -109,7 +123,9 @@ class Handler extends ExceptionHandler
|
|||||||
if ($exception instanceof ModelNotFoundException) {
|
if ($exception instanceof ModelNotFoundException) {
|
||||||
// ajax 404 json feedback
|
// ajax 404 json feedback
|
||||||
if ($request->ajax()) {
|
if ($request->ajax()) {
|
||||||
return response()->json(['error' => 'Not Found'], 404);
|
return response()->json([
|
||||||
|
'error' => trans('errors.header.404'),
|
||||||
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -130,7 +146,9 @@ class Handler extends ExceptionHandler
|
|||||||
if ($exception instanceof FatalThrowableError) {
|
if ($exception instanceof FatalThrowableError) {
|
||||||
// ajax 500 json feedback
|
// ajax 500 json feedback
|
||||||
if ($request->ajax()) {
|
if ($request->ajax()) {
|
||||||
return response()->json(['error' => 'Error Page'], 500);
|
return response()->json([
|
||||||
|
'error' => trans('errors.header.500'),
|
||||||
|
], 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
// normal 500 view page feedback
|
// normal 500 view page feedback
|
||||||
@ -140,7 +158,25 @@ class Handler extends ExceptionHandler
|
|||||||
if ($exception instanceof ThrottleRequestsException) {
|
if ($exception instanceof ThrottleRequestsException) {
|
||||||
// ajax 500 json feedback
|
// ajax 500 json feedback
|
||||||
if ($request->ajax()) {
|
if ($request->ajax()) {
|
||||||
return response()->json(['error' => $exception->getMessage()], 429);
|
return response()->json([
|
||||||
|
'error' => $exception->getMessage(),
|
||||||
|
], 429);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($exception instanceof ViewException) {
|
||||||
|
$real_exception = $this->getRealException($exception, ViewException::class);
|
||||||
|
|
||||||
|
if ($real_exception instanceof UnexpectedAmountException) {
|
||||||
|
if ($request->ajax()) {
|
||||||
|
return response()->json([
|
||||||
|
'error' => trans('errors.message.amount'),
|
||||||
|
], 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->view('errors.500', [
|
||||||
|
'message' => trans('errors.message.amount'),
|
||||||
|
], 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,10 +309,6 @@ class Handler extends ExceptionHandler
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the headers from the exception.
|
* Get the headers from the exception.
|
||||||
*
|
|
||||||
* @param Throwable $exception
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
protected function getHeaders(Throwable $exception): array
|
protected function getHeaders(Throwable $exception): array
|
||||||
{
|
{
|
||||||
@ -284,4 +316,18 @@ class Handler extends ExceptionHandler
|
|||||||
? $exception->getHeaders()
|
? $exception->getHeaders()
|
||||||
: [];
|
: [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the real exception.
|
||||||
|
*/
|
||||||
|
protected function getRealException(Throwable $exception, string $current): Throwable
|
||||||
|
{
|
||||||
|
$previous = $exception->getPrevious() ?? $exception;
|
||||||
|
|
||||||
|
while (($previous instanceof $current) && $previous->getPrevious()) {
|
||||||
|
$previous = $previous->getPrevious();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $previous;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,5 +19,7 @@ return [
|
|||||||
'404' => 'We could not find the page you were looking for.',
|
'404' => 'We could not find the page you were looking for.',
|
||||||
'500' => 'We will work on fixing that right away.',
|
'500' => 'We will work on fixing that right away.',
|
||||||
'record' => 'We could not find the record you were looking for.',
|
'record' => 'We could not find the record you were looking for.',
|
||||||
|
'amount' => 'This page contains invalid amounts! Please, contact the system administrator.',
|
||||||
],
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
@ -14,6 +14,12 @@
|
|||||||
{{ trans('errors.title.403') }}
|
{{ trans('errors.title.403') }}
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
|
@if (! empty($message))
|
||||||
|
<span class="text-lg">
|
||||||
|
{{ $message }}
|
||||||
|
</span>
|
||||||
|
@endif
|
||||||
|
|
||||||
@php $landing_page = user() ? user()->getLandingPageOfUser() : route('login'); @endphp
|
@php $landing_page = user() ? user()->getLandingPageOfUser() : route('login'); @endphp
|
||||||
<a href="{{ $landing_page }}" class="relative flex items-center justify-center bg-green hover:bg-green-700 text-white px-6 py-1.5 text-base rounded-lg disabled:bg-green-100 mt-3">
|
<a href="{{ $landing_page }}" class="relative flex items-center justify-center bg-green hover:bg-green-700 text-white px-6 py-1.5 text-base rounded-lg disabled:bg-green-100 mt-3">
|
||||||
{{ trans('general.go_to_dashboard') }}
|
{{ trans('general.go_to_dashboard') }}
|
||||||
|
@ -14,6 +14,12 @@
|
|||||||
{{ trans('errors.title.404') }}
|
{{ trans('errors.title.404') }}
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
|
@if (! empty($message))
|
||||||
|
<span class="text-lg">
|
||||||
|
{{ $message }}
|
||||||
|
</span>
|
||||||
|
@endif
|
||||||
|
|
||||||
@php $landing_page = user() ? user()->getLandingPageOfUser() : route('login'); @endphp
|
@php $landing_page = user() ? user()->getLandingPageOfUser() : route('login'); @endphp
|
||||||
<a href="{{ $landing_page }}" class="relative flex items-center justify-center bg-green hover:bg-green-700 text-white px-6 py-1.5 text-base rounded-lg disabled:bg-green-100 mt-3">
|
<a href="{{ $landing_page }}" class="relative flex items-center justify-center bg-green hover:bg-green-700 text-white px-6 py-1.5 text-base rounded-lg disabled:bg-green-100 mt-3">
|
||||||
{{ trans('general.go_to_dashboard') }}
|
{{ trans('general.go_to_dashboard') }}
|
||||||
|
@ -14,6 +14,12 @@
|
|||||||
{{ trans('errors.title.500') }}
|
{{ trans('errors.title.500') }}
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
|
@if (! empty($message))
|
||||||
|
<span class="text-lg">
|
||||||
|
{{ $message }}
|
||||||
|
</span>
|
||||||
|
@endif
|
||||||
|
|
||||||
@php $landing_page = user() ? user()->getLandingPageOfUser() : route('login'); @endphp
|
@php $landing_page = user() ? user()->getLandingPageOfUser() : route('login'); @endphp
|
||||||
<a href="{{ $landing_page }}" class="relative flex items-center justify-center bg-green hover:bg-green-700 text-white px-6 py-1.5 text-base rounded-lg disabled:bg-green-100 mt-3">
|
<a href="{{ $landing_page }}" class="relative flex items-center justify-center bg-green hover:bg-green-700 text-white px-6 py-1.5 text-base rounded-lg disabled:bg-green-100 mt-3">
|
||||||
{{ trans('general.go_to_dashboard') }}
|
{{ trans('general.go_to_dashboard') }}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user