diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 8915373ed..b238b099e 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -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, \Psr\Log\LogLevel::*> + */ + protected $levels = [ + // + ]; + /** * A list of the exception types that are not reported. * - * @var array + * @var array> */ 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 */ 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; + } } diff --git a/resources/lang/en-GB/errors.php b/resources/lang/en-GB/errors.php index dac3aa6f2..b9e41150f 100644 --- a/resources/lang/en-GB/errors.php +++ b/resources/lang/en-GB/errors.php @@ -19,5 +19,7 @@ return [ '404' => 'We could not find the page you were looking for.', '500' => 'We will work on fixing that right away.', 'record' => 'We could not find the record you were looking for.', + 'amount' => 'This page contains invalid amounts! Please, contact the system administrator.', ], + ]; diff --git a/resources/views/errors/403.blade.php b/resources/views/errors/403.blade.php index 7f2a06aea..0010199e2 100644 --- a/resources/views/errors/403.blade.php +++ b/resources/views/errors/403.blade.php @@ -14,6 +14,12 @@ {{ trans('errors.title.403') }} + @if (! empty($message)) + + {{ $message }} + + @endif + @php $landing_page = user() ? user()->getLandingPageOfUser() : route('login'); @endphp {{ trans('general.go_to_dashboard') }} diff --git a/resources/views/errors/404.blade.php b/resources/views/errors/404.blade.php index c35fd01e2..aa278940a 100644 --- a/resources/views/errors/404.blade.php +++ b/resources/views/errors/404.blade.php @@ -14,6 +14,12 @@ {{ trans('errors.title.404') }} + @if (! empty($message)) + + {{ $message }} + + @endif + @php $landing_page = user() ? user()->getLandingPageOfUser() : route('login'); @endphp {{ trans('general.go_to_dashboard') }} diff --git a/resources/views/errors/500.blade.php b/resources/views/errors/500.blade.php index 5ef9f5b4a..12f037609 100644 --- a/resources/views/errors/500.blade.php +++ b/resources/views/errors/500.blade.php @@ -14,6 +14,12 @@ {{ trans('errors.title.500') }} + @if (! empty($message)) + + {{ $message }} + + @endif + @php $landing_page = user() ? user()->getLandingPageOfUser() : route('login'); @endphp {{ trans('general.go_to_dashboard') }}