Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel 8/9 #58

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
composer.phar
composer.lock
composer.lock
/.idea
43 changes: 25 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,15 @@ An easy way to send emails with stack trace whenever an exception occurs on the

### Install via Composer

#### For Laravel <= 5.2, please use the [v1 branch](https://github.com/squareboat/sneaker/tree/v1)!
#### For Laravel 5.2 < version <= 6.x, please use the [v5 branch](https://github.com/squareboat/sneaker/tree/v5)!
#### For Laravel 7.x, please use the [v7 branch](https://github.com/squareboat/sneaker/tree/v7)!
#### For Laravel 8.x and up, please use the [main branch](https://github.com/squareboat/sneaker)!

```
$ composer require squareboat/sneaker
```

### Configure Laravel

> If you are using __laravel 5.5__ or higher you should skip this step.

If you are using laravel 5.3 or 5.4, simply add the service provider to your project's `config/app.php` file:

#### Service Provider
```
SquareBoat\Sneaker\SneakerServiceProvider::class,
```

### Add Sneaker's Exception Capturing

#### For Laravel < 11
Add exception capturing to `app/Exceptions/Handler.php`:

```php
Expand All @@ -39,6 +28,25 @@ public function report(Exception $exception)
}
```

#### For Laravel 11.x
Add exception capturing to `bootstrap/app.php`:

```php
return Application::configure(basePath: dirname(__DIR__))

(...)

->withExceptions(function (Exceptions $exceptions) {

$exceptions->report(function (\Throwable $e){
app('sneaker')->captureException($e);
});

})->create();
```



### Configuration File

Create the Sneaker configuration file with this command:
Expand Down Expand Up @@ -97,12 +105,11 @@ public function report(Exception $exception)

#### to

This is the list of recipients of error emails.
This is the list of recipients of error emails. Set it in `SNEAKER_TO=` in your `.env` file.
Multiple addresses can be added as a comma-separated list.

```php
'to' => [
// '[email protected]',
],
'to' => explode(',', env('SNEAKER_TO', '')),
```

#### ignored_bots
Expand Down
18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.*",
"illuminate/view": "5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.*",
"illuminate/config": "5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.*",
"illuminate/mail": "5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.*",
"illuminate/log": "5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.*",
"symfony/debug": "~3.1|~3.2|~4.0"
"php": ">=7.3",
"illuminate/support": "^8.0 | ^9.0 | ^10.0 | ^11.0",
"illuminate/view": "^8.0 | ^9.0 | ^10.0 | ^11.0",
"illuminate/config": "^8.0 | ^9.0 | ^10.0 | ^11.0",
"illuminate/mail": "^8.0 | ^9.0 | ^10.0 | ^11.0",
"illuminate/log": "^8.0 | ^9.0 | ^10.0 | ^11.0",
"illuminate/queue": "^8.0 | ^9.0 | ^10.0 | ^11.0",
"illuminate/console": "^8.0 | ^9.0 | ^10.0 | ^11.0",
"symfony/error-handler": "~5.1 | ^6.0 | ^7.0"
},
"require-dev": {
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 4 additions & 4 deletions config/sneaker.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Symfony\Component\ErrorHandler\Error\FatalError;

return [

/*
Expand All @@ -24,7 +26,7 @@
|
*/
'capture' => [
Symfony\Component\Debug\Exception\FatalErrorException::class,
FatalError::class,
],

/*
Expand All @@ -36,9 +38,7 @@
|
*/

'to' => [
// '[email protected]',
],
'to' => explode(',', env('SNEAKER_TO', '')),

/*
|--------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions resources/views/email/body.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
<div class="extra-info">
&#128336; &nbsp;{{ date('l, jS \of F Y h:i:s a') }} {{ date_default_timezone_get() }}
</div>
<div class="extra-info">
User - {{ optional(request()->user())->email ?? 'Guest' }}
</div>
</body>
</html>
8 changes: 4 additions & 4 deletions src/Commands/Sneak.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Sneak extends Command
*
* @var \Illuminate\Config\Repository
*/
private $config;
private Repository $config;

/**
* Create a sneak command instance.
Expand All @@ -49,7 +49,7 @@ public function __construct(Repository $config)
*
* @return void
*/
public function handle()
public function handle(): void
{
$this->overrideConfig();

Expand All @@ -58,13 +58,13 @@ public function handle()

$this->info('Sneaker is working fine ✅');
} catch (Exception $e) {
(new ConsoleApplication)->renderException($e, $this->output);
(new ConsoleApplication)->renderThrowable($e, $this->output);
}
}

/**
* Overriding the default configurations.
*
*
* @return void
*/
public function overrideConfig()
Expand Down
44 changes: 23 additions & 21 deletions src/ExceptionHandler.php → src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
namespace SquareBoat\Sneaker;

use Illuminate\View\Factory;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;

class ExceptionHandler

class ErrorHandler
{
/**
* The view factory implementation.
*
*
* @var \Illuminate\View\Factory
*/
private $view;
private Factory $view;

/**
* Create a new exception handler instance.
Expand All @@ -28,37 +29,38 @@ public function __construct(Factory $view)

/**
* Create a string for the given exception.
*
*
* @param \Exception $exception
* @return string
*/
public function convertExceptionToString($exception)
public function convertExceptionToString($exception): string
{
return $this->view->make('sneaker::email.subject', compact('exception'))->render();
}

/**
* Create a html for the given exception.
*
* @param \Exception $exception
* @param \Exception $exception
* @return string
*/
public function convertExceptionToHtml($exception)
public function convertExceptionToHtml(\Throwable $exception): string
{
$flat = $this->getFlattenedException($exception);

$handler = new SymfonyExceptionHandler();
$renderer = new HtmlErrorRenderer(true);

return $this->decorate($renderer->getBody($flat), $renderer->getStylesheet($flat), $flat);

return $this->decorate($handler->getContent($flat), $handler->getStylesheet($flat), $flat);
}

/**
* Converts the Exception in a PHP Exception to be able to serialize it.
*
* @param \Exception $exception
* @return \Symfony\Component\Debug\Exception\FlattenException
*
* @param $exception
* @return FlattenException
*/
private function getFlattenedException($exception)
private function getFlattenedException($exception): FlattenException
{
if (!$exception instanceof FlattenException) {
$exception = FlattenException::createFromThrowable($exception);
Expand All @@ -70,11 +72,11 @@ private function getFlattenedException($exception)
/**
* Get the html response content.
*
* @param string $content
* @param string $css
* @param string $content
* @param string $css
* @return string
*/
private function decorate($content, $css, $exception)
private function decorate(string $content, string $css, $exception): string
{
$content = $this->removeTitle($content);

Expand All @@ -83,11 +85,11 @@ private function decorate($content, $css, $exception)

/**
* Removes title from content as it is same for all exceptions and has no real value.
*
* @param string $content
*
* @param string $content
* @return string
*/
private function removeTitle($content)
private function removeTitle(string $content): string
{
$titles = [
'Whoops, looks like something went wrong.',
Expand Down
4 changes: 2 additions & 2 deletions src/ExceptionMailer.php → src/ErrorMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ExceptionMailer extends Mailable implements ShouldQueue
class ErrorMailer extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

Expand Down Expand Up @@ -42,7 +42,7 @@ public function __construct($subject, $body)
*
* @return $this
*/
public function build()
public function build(): static
{
return $this->view('sneaker::raw')
->with('content', $this->body);
Expand Down
Loading