akaunting/app/Exceptions/Handler.php

120 lines
3.4 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\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Debug\Exception\FatalThrowableError;
2020-03-04 11:09:28 +03:00
use Throwable;
2017-09-14 22:21:00 +03:00
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @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 (env('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
2018-12-03 13:45:33 +03:00
session(['url.intended' => $request->url()]);
2019-11-16 10:21:14 +03:00
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest($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();
// 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);
}
// 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
}