Skip to content

Commit

Permalink
Merge pull request #189 from spatie/fix-invalid-view-trace
Browse files Browse the repository at this point in the history
Fix view traces
  • Loading branch information
rubenvanassche authored Apr 30, 2024
2 parents b9d935f + 166ad8c commit 866eadf
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Views/ViewExceptionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Spatie\LaravelIgnition\Views;

use Exception;
use Illuminate\Contracts\View\Engine;
use Illuminate\Foundation\Application;
use Illuminate\Support\Arr;
Expand All @@ -19,7 +18,9 @@
class ViewExceptionMapper
{
protected Engine $compilerEngine;

protected BladeSourceMapCompiler $bladeSourceMapCompiler;

protected array $knownPaths;

public function __construct(BladeSourceMapCompiler $bladeSourceMapCompiler)
Expand Down Expand Up @@ -80,15 +81,27 @@ protected function createException(Throwable $baseException): IgnitionViewExcept

protected function modifyViewsInTrace(IgnitionViewException $exception): void
{
$viewIndex = null;

$trace = Collection::make($exception->getPrevious()->getTrace())
->map(function ($trace) {
->map(function ($trace, $index) use (&$viewIndex) {
if ($originalPath = $this->findCompiledView(Arr::get($trace, 'file', ''))) {

$trace['file'] = $originalPath;
$trace['line'] = $this->getBladeLineNumber($trace['file'], $trace['line']);

if ($viewIndex === null) {
$viewIndex = $index;
}
}

return $trace;
})->toArray();
})
->when(
$viewIndex !== null && str_ends_with($exception->getFile(), '.blade.php'),
fn (Collection $trace) => $trace->slice($viewIndex + 1) // Remove all traces before the view
)
->toArray();

$traceProperty = new ReflectionProperty('Exception', 'trace');
$traceProperty->setAccessible(true);
Expand Down

0 comments on commit 866eadf

Please sign in to comment.