Skip to content

Commit

Permalink
Merge pull request #200 from spatie/feature/handling-errors
Browse files Browse the repository at this point in the history
Handling errors
  • Loading branch information
rubenvanassche authored May 2, 2024
2 parents d3ce719 + 97193a0 commit f52124d
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 4 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: PHPStan

on:
push:
paths:
- '**.php'
- 'phpstan.neon.dist'
pull_request:
paths:
- '**.php'
- 'phpstan.neon.dist'


jobs:
phpstan:
name: phpstan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v1

- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ext-json": "*",
"ext-mbstring": "*",
"illuminate/support": "^10.0|^11.0",
"spatie/flare-client-php": "^1.3.5",
"spatie/flare-client-php": "^1.5",
"spatie/ignition": "^1.14",
"symfony/console": "^6.2.3|^7.0",
"symfony/var-dumper": "^6.2.3|^7.0"
Expand Down
4 changes: 3 additions & 1 deletion config/flare.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Spatie\FlareClient\FlareMiddleware\CensorRequestHeaders;
use Spatie\LaravelIgnition\FlareMiddleware\AddDumps;
use Spatie\LaravelIgnition\FlareMiddleware\AddEnvironmentInformation;
use Spatie\LaravelIgnition\FlareMiddleware\AddExceptionHandledStatus;
use Spatie\LaravelIgnition\FlareMiddleware\AddExceptionInformation;
use Spatie\LaravelIgnition\FlareMiddleware\AddJobs;
use Spatie\LaravelIgnition\FlareMiddleware\AddLogs;
Expand Down Expand Up @@ -55,6 +56,7 @@
'max_chained_job_reporting_depth' => 5,
],
AddContext::class,
AddExceptionHandledStatus::class,
CensorRequestBodyFields::class => [
'censor_fields' => [
'password',
Expand All @@ -70,7 +72,7 @@
'X-CSRF-TOKEN',
'X-XSRF-TOKEN',
]
]
],
],

/*
Expand Down
27 changes: 26 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Livewire\\\\LivewireManager\\:\\:getClass\\(\\)\\.$#"
count: 1
path: src/ContextProviders/LaravelLivewireRequestContextProvider.php

-
message: "#^Cannot call method get\\(\\) on Symfony\\\\Component\\\\HttpFoundation\\\\Request\\|null\\.$#"
count: 1
path: src/ContextProviders/LaravelLivewireRequestContextProvider.php

-
message: "#^Cannot call method has\\(\\) on Symfony\\\\Component\\\\HttpFoundation\\\\Request\\|null\\.$#"
count: 1
path: src/ContextProviders/LaravelLivewireRequestContextProvider.php

-
message: "#^Method Spatie\\\\LaravelIgnition\\\\ContextProviders\\\\LaravelLivewireRequestContextProvider\\:\\:resolveUpdates\\(\\) has parameter \\$updates with no value type specified in iterable type array\\.$#"
count: 1
path: src/ContextProviders/LaravelLivewireRequestContextProvider.php

-
message: "#^Cannot call method toFlare\\(\\) on class\\-string\\|object\\.$#"
count: 1
Expand All @@ -25,6 +45,11 @@ parameters:
count: 1
path: src/Exceptions/InvalidConfig.php

-
message: "#^Class Livewire\\\\LivewireComponentsFinder not found\\.$#"
count: 1
path: src/Solutions/LivewireDiscoverSolution.php

-
message: "#^Parameter \\#1 \\$invalidController of method Spatie\\\\LaravelIgnition\\\\Solutions\\\\SolutionProviders\\\\InvalidRouteActionSolutionProvider\\:\\:findRelatedController\\(\\) expects string, string\\|null given\\.$#"
count: 1
Expand All @@ -46,7 +71,7 @@ parameters:
path: src/Solutions/SolutionProviders/UnknownValidationSolutionProvider.php

-
message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\<int,ReflectionMethod\\>\\:\\:filter\\(\\) expects \\(callable\\(ReflectionMethod, int\\)\\: bool\\)\\|null, Closure\\(ReflectionMethod\\)\\: 0\\|1\\|false given\\.$#"
message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\<int,ReflectionMethod\\>\\:\\:filter\\(\\) expects \\(callable\\(ReflectionMethod, int\\)\\: bool\\)\\|null, Closure\\(ReflectionMethod\\)\\: \\(0\\|1\\|false\\) given\\.$#"
count: 1
path: src/Solutions/SolutionProviders/UnknownValidationSolutionProvider.php

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function toArray(): array
return $properties;
}

/** @return array<string, mixed> */
/** @return array<int, mixed> */
protected function getLivewireInformation(): array
{
if ($this->request->has('components')) {
Expand Down
53 changes: 53 additions & 0 deletions src/FlareMiddleware/AddExceptionHandledStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Spatie\LaravelIgnition\FlareMiddleware;

use Closure;
use Spatie\Backtrace\Backtrace;
use Spatie\FlareClient\FlareMiddleware\FlareMiddleware;
use Spatie\FlareClient\Report;
use Throwable;

class AddExceptionHandledStatus implements FlareMiddleware
{
public function handle(Report $report, Closure $next)
{
$frames = Backtrace::create()->limit(40)->frames();
$frameCount = count($frames);

try {
foreach ($frames as $i => $frame) {
// Check first frame, probably Illuminate\Foundation\Exceptions\Handler::report()
// Next frame should be: Illuminate/Foundation/helpers.php::report()

if ($frame->method !== 'report') {
continue;
}

if ($frame->class === null) {
continue;
}

if ($i === $frameCount - 1) {
continue;
}

if ($frames[$i + 1]->class !== null) {
continue;
}

if ($frames[$i + 1]->method !== 'report') {
continue;
}

$report->handled();

break;
}
} catch (Throwable) {
// Do nothing
}

return $next($report);
}
}
35 changes: 35 additions & 0 deletions tests/FlareMiddleware/AddExceptionHandledTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Contracts\Debug\ExceptionHandler;
use Orchestra\Testbench\Exceptions\Handler;
use Spatie\FlareClient\Report;
use Spatie\LaravelIgnition\Facades\Flare;

it('can see when an exception is handled, meaning it is reported', function () {
$handler = new class(app()) extends Handler {
public static Report $report;

public function report(Throwable $e)
{
self::$report = Flare::createReport($e);
}
};

app()->bind(ExceptionHandler::class, fn () => $handler);

$someTriggeredException = new Exception('This is a test exception');

report($someTriggeredException);

expect($handler::$report)->toBeInstanceOf(Report::class);
expect($handler::$report->toArray())
->toHaveKey('handled', true);
});

it('will not mark an exception handled when it is not', function () {
$someTriggeredException = new Exception('This is a test exception');

$report = Flare::createReport($someTriggeredException);

expect($report->toArray())->toHaveKey('handled', null);
});

0 comments on commit f52124d

Please sign in to comment.