Skip to content

Commit

Permalink
Add support for laravel 11 error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Apr 29, 2024
1 parent e04f998 commit 514260f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 42 deletions.
93 changes: 69 additions & 24 deletions src/Commands/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use ReflectionProperty;
use Spatie\FlareClient\Flare;
use Spatie\FlareClient\Http\Exceptions\BadResponseCode;
use Spatie\LaravelIgnition\Support\LaravelFlare;

class TestCommand extends Command
{
Expand Down Expand Up @@ -52,26 +51,12 @@ protected function checkFlareKey(): self

public function checkFlareLogger(): self
{
if (! $this->hasFlareReportableCallback()) {
$defaultLogChannel = $this->config->get('logging.default');
$configuredCorrectly = $this->shouldUseReportableCallbackLogger()
? $this->checkFlareReportableCallbackLogger()
: $this->checkFlareConfigLogger();

$activeStack = $this->config->get("logging.channels.{$defaultLogChannel}");

if (is_null($activeStack)) {
$this->info("❌ The default logging channel `{$defaultLogChannel}` is not configured in the `logging` config file");
}

if (! isset($activeStack['channels']) || ! in_array('flare', $activeStack['channels'])) {
$this->info("❌ The logging channel `{$defaultLogChannel}` does not contain the 'flare' channel");
}

if (is_null($this->config->get('logging.channels.flare'))) {
$this->info('❌ There is no logging channel named `flare` in the `logging` config file');
}

if ($this->config->get('logging.channels.flare.driver') !== 'flare') {
$this->info('❌ The `flare` logging channel defined in the `logging` config file is not set to `flare`.');
}
if($configuredCorrectly === false) {
die();
}

if ($this->config->get('ignition.with_stack_frame_arguments') && ini_get('zend.exception_ignore_args')) {
Expand All @@ -83,12 +68,72 @@ public function checkFlareLogger(): self
return $this;
}

protected function hasFlareReportableCallback(): bool
protected function shouldUseReportableCallbackLogger(): bool
{
if (version_compare(app()->version(), '11.0.0', '<')) {
return false;
return version_compare(app()->version(), '11.0.0', '>=');
}

protected function checkFlareConfigLogger(): bool
{
$failures = $this->resolveFlareConfigLoggerFailures();

foreach ($failures as $failure) {
$this->info($failure);
}

return empty($failures);
}

protected function resolveFlareConfigLoggerFailures(): array
{
$defaultLogChannel = $this->config->get('logging.default');

$activeStack = $this->config->get("logging.channels.{$defaultLogChannel}");

$failures = [];

if (is_null($activeStack)) {
$failures[] = "❌ The default logging channel `{$defaultLogChannel}` is not configured in the `logging` config file";
}

if (! isset($activeStack['channels']) || ! in_array('flare', $activeStack['channels'])) {
$failures[] = "❌ The logging channel `{$defaultLogChannel}` does not contain the 'flare' channel";
}

if (is_null($this->config->get('logging.channels.flare'))) {
$failures[] = '❌ There is no logging channel named `flare` in the `logging` config file';
}

if ($this->config->get('logging.channels.flare.driver') !== 'flare') {
$failures[] = '❌ The `flare` logging channel defined in the `logging` config file is not set to `flare`.';
}

return $failures;
}

protected function checkFlareReportableCallbackLogger(): bool
{
if ($this->hasFlareReportableCallbackLogger()) {
return true;
}

if(empty($this->resolveFlareConfigLoggerFailures())){
return true;
}

$this->info('❌ The Flare logging driver was not configured correctly.');
$this->newLine();
$this->info('<fg=default;bg=default>Please ensure the following code is present in your `<fg=green>bootstrap/app.php</>` file:</>');
$this->newLine();
$this->info('<fg=default;bg=default>-><fg=green>withExceptions</>(<fg=blue>function</> (<fg=red>Exceptions</> $exceptions) {</>');
$this->info('<fg=default;bg=default> <fg=red>Flare</>::<fg=green>handles</>($exceptions);</>');
$this->info('<fg=default;bg=default>})-><fg=green>create</>();</>');

return false;
}

protected function hasFlareReportableCallbackLogger(): bool
{
try {
$handler = app(Handler::class);

Expand All @@ -110,7 +155,7 @@ protected function hasFlareReportableCallback(): bool
$reflection = new ReflectionClosure($callback);
$closureReturnTypeReflection = $reflection->getReturnType();

if(! $closureReturnTypeReflection instanceof ReflectionNamedType){
if (! $closureReturnTypeReflection instanceof ReflectionNamedType) {
return false;
}

Expand Down
16 changes: 15 additions & 1 deletion src/Facades/Flare.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Spatie\LaravelIgnition\Facades;

use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Support\Facades\Facade;
use Spatie\FlareClient\Flare as FlareClient;
use Spatie\LaravelIgnition\Support\SentReports;
use Throwable;

/**
* @method static void glow(string $name, string $messageLevel = \Spatie\FlareClient\Enums\MessageLevels::INFO, array $metaData = [])
Expand All @@ -16,7 +19,18 @@ class Flare extends Facade
{
protected static function getFacadeAccessor()
{
return \Spatie\LaravelIgnition\Support\LaravelFlare::class;
return FlareClient::class;
}

public static function handles(Exceptions $exceptions): void
{
$exceptions->reportable(static function (Throwable $exception): FlareClient {
$flare = app(FlareClient::class);

$flare->report($exception);

return $flare;
});
}

public static function sentReports(): SentReports
Expand Down
17 changes: 0 additions & 17 deletions src/Support/LaravelFlare.php

This file was deleted.

0 comments on commit 514260f

Please sign in to comment.