deplay phase 1

This commit is contained in:
Kevin Adametz 2026-04-17 17:19:11 +02:00
parent e3dc1afd8e
commit 5a7478907e
68 changed files with 2831 additions and 818 deletions

View file

@ -2,7 +2,16 @@
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
@ -37,6 +46,65 @@ class Handler extends ExceptionHandler
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';
}
/**
@ -52,4 +120,28 @@ class Handler extends ExceptionHandler
{
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
);
}
}
}