147 lines
4 KiB
PHP
Executable file
147 lines
4 KiB
PHP
Executable file
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
use Illuminate\Session\TokenMismatchException;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
|
|
use Symfony\Component\ErrorHandler\Exception\FlattenException;
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
|
use Throwable;
|
|
|
|
class Handler extends ExceptionHandler
|
|
{
|
|
/**
|
|
* A list of the exception types that are not reported.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dontReport = [
|
|
//
|
|
];
|
|
|
|
/**
|
|
* A list of the inputs that are never flashed for validation exceptions.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dontFlash = [
|
|
'password',
|
|
'password_confirmation',
|
|
];
|
|
|
|
/**
|
|
* Report or log an exception.
|
|
*
|
|
* @param \Throwable $exception
|
|
* @return void
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function report(Throwable $exception)
|
|
{
|
|
parent::report($exception);
|
|
|
|
$e = $this->mapException($exception);
|
|
|
|
if ($this->shouldMailServerError($e)) {
|
|
$this->sendEmail($e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* E-Mail nur bei echten Serverfeilern (5xx bzw. unbehandelte Exceptions), nicht bei lokalem Entwickeln.
|
|
*/
|
|
protected function shouldMailServerError(Throwable $e): bool
|
|
{
|
|
if (app()->environment('local', 'testing')) {
|
|
return false;
|
|
}
|
|
|
|
if (! config('app.exception_mail')) {
|
|
return false;
|
|
}
|
|
|
|
return $this->exceptionIndicatesServerError($e);
|
|
}
|
|
|
|
/**
|
|
* Entspricht dem, was üblicherweise als HTTP500 ausgeliefert würde.
|
|
*/
|
|
protected function exceptionIndicatesServerError(Throwable $e): bool
|
|
{
|
|
if (
|
|
$e instanceof AuthenticationException
|
|
|| $e instanceof AuthorizationException
|
|
|| $e instanceof ModelNotFoundException
|
|
|| $e instanceof ValidationException
|
|
|| $e instanceof TokenMismatchException
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
if ($e instanceof HttpExceptionInterface) {
|
|
return $e->getStatusCode() >= 500;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function exceptionMailContextLine(): string
|
|
{
|
|
if (app()->runningInConsole()) {
|
|
$argv = $_SERVER['argv'] ?? [];
|
|
|
|
return 'CLI: ' . (count($argv) ? implode(' ', $argv) : php_sapi_name());
|
|
}
|
|
|
|
if (app()->bound('request') && request()) {
|
|
return request()->fullUrl();
|
|
}
|
|
|
|
return 'n/a';
|
|
}
|
|
|
|
/**
|
|
* Render an exception into an HTTP response.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Throwable $exception
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
*
|
|
* @throws \Throwable
|
|
*/
|
|
public function render($request, Throwable $exception)
|
|
{
|
|
return parent::render($request, $exception);
|
|
}
|
|
|
|
public function sendEmail(Throwable $exception)
|
|
{
|
|
try {
|
|
$e = FlattenException::create($exception);
|
|
$handler = new HtmlErrorRenderer(true);
|
|
$css = $handler->getStylesheet();
|
|
$content = $handler->getBody($e);
|
|
$to = config('app.exception_mail');
|
|
$subject = config('app.name') . ' Exception: ' . $this->exceptionMailContextLine();
|
|
|
|
if ($to) {
|
|
Mail::send('emails.exception', compact('css', 'content'), function ($message) use ($to, $subject) {
|
|
$message->to($to)->subject($subject);
|
|
});
|
|
}
|
|
} catch (Throwable $ex) {
|
|
file_put_contents(
|
|
storage_path('logs/laravel-' . date('Y-m-d') . '.log'),
|
|
'[' . date('Y-m-d H:i:s') . '] exception-handler-error: ' . $ex->getMessage() . "\n",
|
|
FILE_APPEND
|
|
);
|
|
}
|
|
}
|
|
}
|