generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from spatie/feature/handling-errors
Handling errors
- Loading branch information
Showing
7 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |