app/Exceptions/Handler.php
<?php
namespace App\Exceptions;
use App;
use Config;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Validation\ValidationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Mail;
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
*/
public function report(Exception $e)
{
if (Config::get('exception.mail')) {
$bMail = true;
foreach (Config::get('exception.exclude') as $sException) {
if ($e instanceof $sException) {
$bMail = false;
break;
}
}
if ($bMail) {
$aRecipients = Config::get('exception.recipients');
$sDomain = parse_url(Config::get('app.url'), PHP_URL_HOST);
$sFromAddress = Config::get('mail.from.address');
$sFromName = Config::get('mail.from.name');
$sSubject = $sDomain.' - Exception in '.pathinfo($e->getFile(), PATHINFO_FILENAME). ' line ' .$e->getLine();
Mail::send('emails.exception', [
'e' => $e,
'sDomain' => $sDomain,
'sEnvironment' => App::environment(),
'sRequestUri' => $_SERVER['REQUEST_URI'],
], function ($m) use ($sFromAddress, $sFromName, $aRecipients, $sSubject) {
$m->from($sFromAddress, $sFromName);
foreach ($aRecipients as $aRecipient) {
$m->to($aRecipient['address'], $aRecipient['name']);
}
$m->subject($sSubject);
});
}
}
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)
{
return parent::render($request, $e);
}
}
config/exception.php
<?php
return [
'recipients' => [
[
'name' => 'David van der Tuijn',
'address' => 'your-email@domain.tld'
]
],
'exclude' => [
'Symfony\Component\HttpKernel\Exception\NotFoundHttpException'
],
'mail' => false
];
resources/views/emails/exception.blade.php
<p><strong>Domain:</strong> {{ $sDomain }}</p>
@if ($sRequestUri)
<p><strong>Request URI:</strong> {{ $sRequestUri }}</p>
@endif
<p><strong>Date:</strong> {{ date('d-m-Y H:i') }}</p>
@if ($e->getMessage())
<p><strong>Message:</strong> {{ $e->getMessage() }}</p>
@endif
@if ($e->getCode() != 0)
<p><strong>Code:</strong> {{ $e->getCode() }}</p>
@endif
<p><strong>Class:</strong> {{ get_class($e) }}</p>
<p><strong>File:</strong> {{ $e->getFile() }}</p>
<p><strong>Line:</strong> {{ $e->getLine() }}</p>
<p><strong>Environment:</strong> {{ $sEnvironment }}</p>
<p><strong>Stack trace:</strong></p>
<pre>{{ $e->getTraceAsString() }}</pre>