File: D:/HostingSpaces/ZelfVerkopen/zelfverkopen.nl/app/Exceptions/Handler.php
<?php
namespace App\Exceptions;
use App\KommaApp\Pages\PageService;
use App\Mail\ErrorAdminMail;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Facades\Config;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported by mail.
*
* @var array
*/
protected $excludeExceptionsFromMail = [
AuthenticationException::class,
AuthorizationException::class,
TokenMismatchException::class,
ValidationException::class
];
/**
* Report or log an exception.
*
* @param Exception $exception
* @return mixed|void
* @throws Exception
*/
public function report(Exception $exception)
{
if (app()->bound('sentry') && $this->shouldReport($exception)) {
app('sentry')->captureException($exception);
}
parent::report($exception);
}
/**
* Send mail to admin
*
* @param $exception
* @return bool
*/
private function mailAdmin($exception)
{
$code = 500;
if (method_exists($exception, 'getStatusCode')) $code = $exception->getStatuscode();
$request = request();
$requestUri = $request->getRequestUri();
// Ignore case
$requestUri = strtolower($requestUri);
// Check for mail exceptions like bots
if($this->isMailException($code, $requestUri)) return false;
//Backup mail configuration
$originalMailConfig = Config::get('mail');
//Configure the mail settings to be different for the exception mail
$expectionMailConfig = $originalMailConfig; //Copy array
$expectionMailConfig['driver'] = $originalMailConfig['exception_driver'];
$expectionMailConfig['host'] = $originalMailConfig['exception_host'];
$expectionMailConfig['port'] = $originalMailConfig['exception_port'];
$expectionMailConfig['from'] = $originalMailConfig['exception_from'];
$expectionMailConfig['admin'] = $originalMailConfig['exception_admin'];
$expectionMailConfig['encryption'] = $originalMailConfig['exception_encryption'];
$expectionMailConfig['username'] = $originalMailConfig['exception_username'];
$expectionMailConfig['password'] = $originalMailConfig['exception_password'];
Config::set('mail', $expectionMailConfig);
\Mail::send(new ErrorAdminMail($code, $requestUri, $exception));
//Restore the original mail configuration
Config::set('mail', $originalMailConfig);
}
/**
* Check if given uri is excluded from mailing to the admin
*
* @param $code
* @param $requestUri
* @return bool
*/
private function isMailException($code, $requestUri){
if ($code == 404) {
return true;
// Check for strings to ignore
foreach(Config::get('errorIgnore.404') as $string)
{
if(str_contains($requestUri,$string)) return true;
}
}
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 true;
}
}
// Check for strings to ignore
foreach(Config::get('errorIgnore.all') as $string)
{
if(str_contains($requestUri,$string)) return true;
}
}
/**
* Render the given HttpException.
*
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function renderHttpException(HttpException $e)
{
$status = $e->getStatusCode();
$paths = collect(config('view.paths'));
view()->replaceNamespace('errors', $paths->map(function ($path) {
return "{$path}/errors";
})->push(__DIR__.'/views')->all());
$pageService = new PageService();
$links = $pageService->getAllTranslatedPageRoutes();
$data = [
'exception' => $e,
'code' => $status,
'links' => $links,
'message' => $e->getMessage(),
];
if (view()->exists($view = "errors::{$status}")) {
return response()->view($view, $data, $status, $e->getHeaders());
}
return $this->convertExceptionToResponse($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)
{
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
//Render the page
return parent::render($request, $e);
}
/**
* 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'));
}
}