diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index f9644add..a9c5f579 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -2,7 +2,9 @@ namespace App\Exceptions; +use App\Mail\ExceptionOccured; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Illuminate\Support\Facades\Log; use Throwable; class Handler extends ExceptionHandler @@ -34,7 +36,30 @@ class Handler extends ExceptionHandler public function register() { $this->reportable(function (Throwable $e) { - // + $this->sendEmail($e); }); } + + /** + * Sends an email upon exception. + * + * @param \Throwable $exception + * + * @return void + */ + public function sendEmail(Throwable $exception) + { + try { + $content['message'] = $exception->getMessage(); + $content['file'] = $exception->getFile(); + $content['line'] = $exception->getLine(); + $content['trace'] = $exception->getTrace(); + $content['url'] = request()->url(); + $content['body'] = request()->all(); + $content['ip'] = request()->ip(); + \Illuminate\Support\Facades\Mail::send(new ExceptionOccured($content)); + } catch (Throwable $exception) { + Log::error($exception); + } + } } diff --git a/app/Http/Livewire/FindAGig.php b/app/Http/Livewire/FindAGig.php index 6f9ab89b..ed790630 100644 --- a/app/Http/Livewire/FindAGig.php +++ b/app/Http/Livewire/FindAGig.php @@ -26,7 +26,7 @@ public function render() public function updatedSearch() { $this->commissions = CommissionPreset::where('title', 'LIKE', "%{$this->search}%") - ->orderBy('rating', 'DESC') +// ->orderBy('rating', 'DESC') ->orderBy('price', 'ASC') ->paginate($this->count); } diff --git a/app/Mail/ExceptionOccured.php b/app/Mail/ExceptionOccured.php new file mode 100644 index 00000000..740f428a --- /dev/null +++ b/app/Mail/ExceptionOccured.php @@ -0,0 +1,66 @@ +content = $content; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + $emailsTo = str_getcsv(config('exceptions.emailExceptionsTo'), ','); + $ccEmails = str_getcsv(config('exceptions.emailExceptionCCto'), ','); + $bccEmails = str_getcsv(config('exceptions.emailExceptionBCCto'), ','); + $fromSender = config('exceptions.emailExceptionFrom'); + $subject = config('exceptions.emailExceptionSubject'); + + if ($emailsTo[0] === null) { + $emailsTo = config('exceptions.emailExceptionsToDefault'); + } + + if ($ccEmails[0] === null) { + $ccEmails = config('exceptions.emailExceptionCCtoDefault'); + } + + if ($bccEmails[0] === null) { + $bccEmails = config('exceptions.emailExceptionBCCtoDefault'); + } + + if (! $fromSender) { + $fromSender = config('exceptions.emailExceptionFromDefault'); + } + + if (! $subject) { + $subject = config('exceptions.emailExceptionSubjectDefault'); + } + + return $this->from($fromSender) + ->to($emailsTo) + ->cc($ccEmails) + ->bcc($bccEmails) + ->subject($subject) + ->view(config('exceptions.emailExceptionView')) + ->with('content', $this->content); + } +} diff --git a/composer.json b/composer.json index 0d8b408e..2ba238ee 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "fruitcake/laravel-cors": "^2.0", "google/recaptcha": "^1.2", "guzzlehttp/guzzle": "^7.0.1", + "jeremykenedy/laravel-exception-notifier": "^3.1", "laravel/cashier": "^12.6", "laravel/framework": "^9.0", "laravel/jetstream": "^2.3.0, !=2.3.1", diff --git a/composer.lock b/composer.lock index ab2f117d..f2f6273a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e312069e5be8ebbe7cddc51e145a6fff", + "content-hash": "2b526514cc89877b173d2ac5b9063aa5", "packages": [ { "name": "asm89/stack-cors", @@ -2503,6 +2503,79 @@ ], "time": "2020-06-13T08:05:20+00:00" }, + { + "name": "jeremykenedy/laravel-exception-notifier", + "version": "v3.1.0", + "source": { + "type": "git", + "url": "https://github.com/jeremykenedy/laravel-exception-notifier.git", + "reference": "02217d7727923d55d172a6d0b1af0bb1589ef86a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/jeremykenedy/laravel-exception-notifier/zipball/02217d7727923d55d172a6d0b1af0bb1589ef86a", + "reference": "02217d7727923d55d172a6d0b1af0bb1589ef86a", + "shasum": "" + }, + "require": { + "php": "^8.0" + }, + "require-dev": { + "laravel/framework": "9.*" + }, + "type": "package", + "extra": { + "laravel": { + "providers": [ + "jeremykenedy\\laravelexceptionnotifier\\LaravelExceptionNotifier" + ] + } + }, + "autoload": { + "psr-4": { + "jeremykenedy\\laravelexceptionnotifier\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "jeremykenedy", + "email": "jeremykenedy@gmail.com" + } + ], + "description": "Laravel exception notifier will send an email of of the error along with the stack trace to the chosen recipients.", + "keywords": [ + "Laravel-Exceptions", + "email", + "email-notification", + "email-notifications", + "exception", + "exception-email", + "exception-emails", + "exception-handler", + "exception-messages", + "laravel", + "notification" + ], + "support": { + "issues": "https://github.com/jeremykenedy/laravel-exception-notifier/issues", + "source": "https://github.com/jeremykenedy/laravel-exception-notifier/tree/v3.1.0" + }, + "funding": [ + { + "url": "https://github.com/jeremykenedy", + "type": "github" + }, + { + "url": "https://www.patreon.com/jeremykenedy", + "type": "patreon" + } + ], + "time": "2022-09-28T06:47:06+00:00" + }, { "name": "laravel/cashier", "version": "v12.17.2", diff --git a/config/exceptions.php b/config/exceptions.php new file mode 100644 index 00000000..8a9137b2 --- /dev/null +++ b/config/exceptions.php @@ -0,0 +1,88 @@ + env('EMAIL_EXCEPTION_ENABLED'), + 'emailExceptionEnabledDefault' => true, + + /* + |-------------------------------------------------------------------------- + | Exception Email From + |-------------------------------------------------------------------------- + | + | This is the email your exception will be from. + | + */ + + 'emailExceptionFrom' => env('EMAIL_EXCEPTION_FROM'), + 'emailExceptionFromDefault' => 'email@email.com', + + /* + |-------------------------------------------------------------------------- + | Exception Email To + |-------------------------------------------------------------------------- + | + | This is the email(s) the exceptions will be emailed to. + | + */ + + 'emailExceptionsTo' => env('EMAIL_EXCEPTION_TO'), + 'emailExceptionsToDefault' => env('MAIL_SUPPORT'), + + /* + |-------------------------------------------------------------------------- + | Exception Email CC + |-------------------------------------------------------------------------- + | + | This is the email(s) the exceptions will be CC emailed to. + | + */ + + 'emailExceptionCCto' => env('EMAIL_EXCEPTION_CC'), + 'emailExceptionCCtoDefault' => [], + + /* + |-------------------------------------------------------------------------- + | Exception Email BCC + |-------------------------------------------------------------------------- + | + | This is the email(s) the exceptions will be BCC emailed to. + | + */ + + 'emailExceptionBCCto' => env('EMAIL_EXCEPTION_BCC'), + 'emailExceptionBCCtoDefault' => [], + + /* + |-------------------------------------------------------------------------- + | Exception Email Subject + |-------------------------------------------------------------------------- + | + | This is the subject of the exception email + | + */ + + 'emailExceptionSubject' => env('EMAIL_EXCEPTION_SUBJECT'), + 'emailExceptionSubjectDefault' => 'Error on '.config('app.env'), + + /* + |-------------------------------------------------------------------------- + | Exception Email View + |-------------------------------------------------------------------------- + | + | This is the view that will be used for the email. + | + */ + + 'emailExceptionView' => 'emails.exception', + +]; diff --git a/resources/views/emails/exception.blade.php b/resources/views/emails/exception.blade.php new file mode 100644 index 00000000..a208fa41 --- /dev/null +++ b/resources/views/emails/exception.blade.php @@ -0,0 +1,81 @@ + + + + + + + + +
+
+
+

{{ $content['message'] ?? '' }}

+
+
+
+
+
+
+ + + + + + + @foreach(($content['trace'] ?? []) as $value) + + + + @endforeach + +
+

+ (1/1) + ErrorException +

+

{{ $content['message'] ?? '' }}

+

URL: {{ $content['url'] ?? '' }}

+

IP: {{ $content['ip'] ?? '' }}

+
+ in {{ $content['file'] ?? '' }} line {{ $content['line'] ?? '' }} +
+ at {{ basename($value['class'] ?? '') }}->{{ $value['function'] ?? '' }}()in {{ $value['file'] ?? '' }} line {{ $value['line'] ?? '' }} +
+
+
+ +