File: D:/HostingSpaces/BVerhoeven/verhoevendak.nl/app/Exceptions/Handler.php
<?php
namespace App\Exceptions;
use App\Helpers\KommaHelpers;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use KommaApp\Core\Facades\Komma;
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 = [
HttpException::class,
ModelNotFoundException::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)
{
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 debug is false. and it is not an httpException, make a 500 exception
*/
if (!env('APP_DEBUG', false) && !$this->isHttpException($e)) {
$e = new \Symfony\Component\HttpKernel\Exception\HttpException(500);
}
//If the mail_debug, is true, mail the admin
//if (env('MAIL_DEBUG', true)) $this->mailAdmin($request, $e, $originalException);
//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*/
// 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)
);
});
}
}