improved exception handler

This commit is contained in:
Denis Duliçi
2022-07-27 14:13:58 +03:00
parent 3883c0ee55
commit 9d7cd71e09
5 changed files with 77 additions and 11 deletions

View File

@ -2,6 +2,7 @@
namespace App\Exceptions;
use Akaunting\Money\Exceptions\UnexpectedAmountException;
use App\Exceptions\Http\Resource as ResourceException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
@ -10,6 +11,7 @@ use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Illuminate\View\ViewException;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@ -17,21 +19,31 @@ use Throwable;
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.
*
* @var array
* @var array<int, class-string<\Throwable>>
*/
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 = [
'current_password',
'password',
'password_confirmation',
];
@ -95,7 +107,9 @@ class Handler extends ExceptionHandler
if ($exception instanceof NotFoundHttpException) {
// ajax 404 json feedback
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();
@ -109,7 +123,9 @@ class Handler extends ExceptionHandler
if ($exception instanceof ModelNotFoundException) {
// ajax 404 json feedback
if ($request->ajax()) {
return response()->json(['error' => 'Not Found'], 404);
return response()->json([
'error' => trans('errors.header.404'),
], 404);
}
try {
@ -130,7 +146,9 @@ class Handler extends ExceptionHandler
if ($exception instanceof FatalThrowableError) {
// ajax 500 json feedback
if ($request->ajax()) {
return response()->json(['error' => 'Error Page'], 500);
return response()->json([
'error' => trans('errors.header.500'),
], 500);
}
// normal 500 view page feedback
@ -140,7 +158,25 @@ class Handler extends ExceptionHandler
if ($exception instanceof ThrottleRequestsException) {
// ajax 500 json feedback
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.
*
* @param Throwable $exception
*
* @return array
*/
protected function getHeaders(Throwable $exception): array
{
@ -284,4 +316,18 @@ class Handler extends ExceptionHandler
? $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;
}
}