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 ); } } }