Files
librenms-librenms/app/Exceptions/Handler.php
T

82 lines
2.5 KiB
PHP
Raw Normal View History

2018-05-09 08:05:17 -05:00
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];
2019-02-21 12:08:35 -06:00
/**
* A list of the exceptions that can be upgraded. Checked in order.
*
* @var array
*/
protected $upgradable = [
\LibreNMS\Exceptions\FilePermissionsException::class,
\LibreNMS\Exceptions\DatabaseConnectException::class,
\LibreNMS\Exceptions\DuskUnsafeException::class,
];
2018-05-09 08:05:17 -05:00
2019-02-21 12:08:35 -06:00
public function render($request, Exception $exception)
2018-05-09 08:05:17 -05:00
{
2019-02-21 12:08:35 -06:00
// If for some reason Blade hasn't been registered, try it now
try {
if (!app()->bound('view')) {
app()->register(\App\Providers\ViewServiceProvider::class);
app()->register(\Illuminate\Translation\TranslationServiceProvider::class);
}
} catch (\Exception $e) {
// continue without view
}
2019-02-21 12:08:35 -06:00
// try to upgrade generic exceptions to more specific ones
foreach ($this->upgradable as $class) {
if ($new = $class::upgrade($exception)) {
return parent::render($request, $new);
}
}
2019-02-21 12:08:35 -06:00
return parent::render($request, $exception);
}
2018-07-17 15:15:11 -05:00
2019-02-21 12:08:35 -06:00
/**
* @param array $convert
* @return Handler
*/
public function setConvert(array $convert): Handler
{
$this->convert = $convert;
return $this;
2018-05-09 08:05:17 -05:00
}
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
2019-02-21 12:08:35 -06:00
return $request->expectsJson() || $request->is('api/*')
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest($exception->redirectTo() ?? route('login'));
}
2018-05-09 08:05:17 -05:00
}