akaunting/app/Exceptions/Handler.php

133 lines
3.8 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Exceptions;
use Illuminate\Auth\AuthenticationException;
2020-03-04 11:09:28 +03:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2017-09-14 22:21:00 +03:00
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\Debug\Exception\FatalThrowableError;
2021-04-05 23:46:52 +03:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2020-03-04 11:09:28 +03:00
use Throwable;
2017-09-14 22:21:00 +03:00
class Handler extends ExceptionHandler
{
/**
2020-10-14 17:07:59 +03:00
* A list of the exception types that are not reported.
2017-09-14 22:21:00 +03:00
*
* @var array
*/
protected $dontReport = [
2019-11-16 10:21:14 +03:00
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
2017-09-14 22:21:00 +03:00
];
/**
* Report or log an exception.
*
2020-03-04 11:09:28 +03:00
* @param \Throwable $exception
2017-09-14 22:21:00 +03:00
* @return void
2020-03-13 15:38:41 +03:00
*
* @throws \Exception
2017-09-14 22:21:00 +03:00
*/
2020-03-04 11:09:28 +03:00
public function report(Throwable $exception)
2017-09-14 22:21:00 +03:00
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
2020-03-04 11:09:28 +03:00
* @param \Throwable $exception
2020-03-13 15:38:41 +03:00
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
2017-09-14 22:21:00 +03:00
*/
2020-03-04 11:09:28 +03:00
public function render($request, Throwable $exception)
2017-09-14 22:21:00 +03:00
{
if (config('app.debug') === false) {
return $this->handleExceptions($request, $exception);
}
2017-09-14 22:21:00 +03:00
return parent::render($request, $exception);
}
/**
2019-11-16 10:21:14 +03:00
* Convert an authentication exception into a response.
2017-09-14 22:21:00 +03:00
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
2019-11-16 10:21:14 +03:00
* @return \Symfony\Component\HttpFoundation\Response
2017-09-14 22:21:00 +03:00
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
2019-11-16 10:21:14 +03:00
// Store the current url in the session
2021-04-05 23:46:52 +03:00
if ($request->url() !== config('app.url')) {
session(['url.intended' => $request->url()]);
}
2018-12-03 13:45:33 +03:00
2019-11-16 10:21:14 +03:00
return $request->expectsJson()
2021-04-05 23:46:52 +03:00
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->to($exception->redirectTo() ?? route('login'));
2017-09-14 22:21:00 +03:00
}
private function handleExceptions($request, $exception)
{
if ($exception instanceof NotFoundHttpException) {
// ajax 404 json feedback
if ($request->ajax()) {
return response()->json(['error' => 'Not Found'], 404);
}
flash(trans('errors.body.page_not_found'))->error()->important();
// normal 404 view page feedback
return redirect()
->back()
->withErrors(['msg', trans('errors.body.page_not_found')]);
}
if ($exception instanceof ModelNotFoundException) {
// ajax 404 json feedback
if ($request->ajax()) {
return response()->json(['error' => 'Not Found'], 404);
}
try {
$names = explode('.', $request->route()->getName());
$names[count($names) - 1] = 'index';
$route = route(implode('.', $names));
flash(trans('errors.message.record'))->warning()->important();
return redirect($route);
} catch (\Exception $e) {
// normal 404 view page feedback
return response()->view('errors.404', [], 404);
}
}
if ($exception instanceof FatalThrowableError) {
// ajax 500 json feedback
if ($request->ajax()) {
return response()->json(['error' => 'Error Page'], 500);
}
// normal 500 view page feedback
return response()->view('errors.500', [], 500);
}
return parent::render($request, $exception);
}
2017-09-14 22:21:00 +03:00
}