Added support for laravel9

This commit is contained in:
Jérémy Munsch 2022-02-03 15:39:21 +01:00
parent 9e17a9c0a4
commit 1ccfb6b148
No known key found for this signature in database
GPG key ID: 55196BEE445F6049
2 changed files with 27 additions and 17 deletions

View file

@ -17,8 +17,8 @@
"Policies"
],
"require": {
"illuminate/support": "~7|~8",
"laravel/framework": "~7|~8"
"illuminate/support": "~7|~8|~9",
"laravel/framework": "~7|~8|~9"
},
"require-dev": {
"phpunit/phpunit": "~9.0",

View file

@ -2,13 +2,10 @@
namespace Kwaadpepper\ExceptionHandler\Exceptions;
use Closure;
use Exception;
use Illuminate\Contracts\Container\Container;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Throwable;
class ExceptionHandler extends Handler
{
@ -22,7 +19,10 @@ class ExceptionHandler extends Handler
public function __construct(Container $container)
{
parent::__construct($container);
if (\strpos(app()->version(), '8') === 0) {
if (
\strpos(app()->version(), '8') === 0 or
\strpos(app()->version(), '9') === 0
) {
$this->reportable($this->reportExceptionByEmail());
}
}
@ -35,7 +35,7 @@ class ExceptionHandler extends Handler
*
* @throws \Exception
*/
public function report(Throwable $e)
public function report(\Throwable $e)
{
if (get_class($e) == RedirectTo::class) {
abort(redirect($e->url));
@ -52,11 +52,11 @@ class ExceptionHandler extends Handler
/**
* Report sending mail closure
*
* @return Closure
* @return \Closure
*/
private function reportExceptionByEmail(): Closure
private function reportExceptionByEmail(): \Closure
{
return function (Throwable $e) {
return function (\Throwable $e) {
$dest = config('exception-handler.contactsList', config('mail.from.address'));
try {
if ($dest and config('exception-handler.enableMaiLog')) {
@ -79,14 +79,24 @@ class ExceptionHandler extends Handler
(request() and request()->fullUrl()) ?
request()->fullUrl() : 'none')
], function ($m) use ($dest) {
/** @var \Illuminate\Mail\Message $m */
$m->to(
$dest,
sprintf('%s Exception Handler', config('app.name'))
)->subject('Need help !, an error occured in your application, please check Logs ASAP');
// ? Is laravel 9 (https://laravel.com/docs/master/upgrade#symfony-mailer)
if(\strpos(app()->version(), '9') !== 0) {
/** @var \Illuminate\Mail\Message $m */
$m->setTo(
$dest,
sprintf('%s Exception Handler', config('app.name'))
)->setSubject('Need help !, an error occured in your application, please check Logs ASAP');
} else {
/** @var \Illuminate\Mail\Message $m */
$m->to(
$dest,
sprintf('%s Exception Handler', config('app.name'))
)->subject('Need help !, an error occured in your application, please check Logs ASAP');
}
});
}
} catch (Exception $e) {
} catch (\ErrorException $e) {
dd($e);
Log::critical('Handler could not send Exception email', ['exception' => $e]);
}
};
@ -110,7 +120,7 @@ class ExceptionHandler extends Handler
* @param \Exception $e
* @return string
*/
private function getAnonymizedStackTrace(Throwable $e): string
private function getAnonymizedStackTrace(\Throwable $e): string
{
$projectPath = \realpath(\sprintf('%s/../', \app_path()));
$traceStack = $e->getTrace();