File: D:/HostingSpaces/SBogers10/somerenslust.komma.pro/app/Exceptions/Handler.php
<?php
namespace App\Exceptions;
use App\Helpers\KommaHelpers;
use App\KommaApp\Pages\PageService;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\KommaApp\Core\Facades\Komma;
use Illuminate\Support\Facades\Config;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
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 = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
dd($e);
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
$originalException = $e;
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
//If the mail_debug, is true, mail the admin
if (env('MAIL_DEBUG', true)) $this->mailAdmin($request, $e, $originalException);
$pageService = new PageService();
$links = $pageService->getAllTranslatedPageRoutes();
\View::share('links', $links);
//Render the page
return parent::render($request, $e);
}
private function mailAdmin($request, $exception, $originalException)
{
// Todo: refactor this
$code = 500;
if (method_exists($exception, 'getStatusCode')) $code = $exception->getStatuscode();
$requestUri = $request->getRequestUri();
// Ignore case
$requestUri = strtolower($requestUri);
$stackTrace = $originalException->__toString();
$errorMessage = $originalException->getMessage();
if ($code == 400) {
/*Custom function for this code*/
}
elseif ($code == 401) {
/*Custom function for this code*/
}
elseif ($code == 403) {
/*Custom function for this code*/
}
elseif ($code == 404) {
/*Custom function for this code*/
return false;
// Check for strings to ignore
foreach(Config::get('errorIgnore.404') as $string)
{
if(str_contains($requestUri,$string)) return false;
}
}
elseif ($code == 500) {
/*Custom function for this code*/
// Check for strings to ignore
foreach(Config::get('errorIgnore.500') as $string)
{
if(str_contains($requestUri,$string)) return false;
}
}
elseif ($code == 501) {
/*Custom function for this code*/
}
elseif ($code == 502) {
/*Custom function for this code*/
}
elseif ($code == 503) {
/*Custom function for this code*/
}
// Check for strings to ignore
foreach(Config::get('errorIgnore.all') as $string)
{
if(str_contains($requestUri,$string)) return false;
}
//Send the email
\Mail::send('emails.admin-error', [
'request' => $request,
'requestUri' => $requestUri,
'stackTrace' => $stackTrace,
'code' => $code,
'errorMessage' => $errorMessage
], function ($m) use ($requestUri, $stackTrace, $code) {
$m->from(Config::get('mail.from.address'), Config::get('mail.from.name'));
$m->to(Config::get('mail.admin.address'))->subject(
$code . ' error on: ' . url($requestUri)
);
});
}
/**
* 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)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
// return redirect()->guest('login');
return redirect()->guest(Config::get('kms.paths.login_route'));
}
}