diff --git a/src/Views/ViewExceptionMapper.php b/src/Views/ViewExceptionMapper.php index 420e8678..a1bb748e 100644 --- a/src/Views/ViewExceptionMapper.php +++ b/src/Views/ViewExceptionMapper.php @@ -2,7 +2,6 @@ namespace Spatie\LaravelIgnition\Views; -use Exception; use Illuminate\Contracts\View\Engine; use Illuminate\Foundation\Application; use Illuminate\Support\Arr; @@ -19,7 +18,9 @@ class ViewExceptionMapper { protected Engine $compilerEngine; + protected BladeSourceMapCompiler $bladeSourceMapCompiler; + protected array $knownPaths; public function __construct(BladeSourceMapCompiler $bladeSourceMapCompiler) @@ -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, + fn (Collection $trace) => $trace->slice($viewIndex + 1) // Remove all traces before the view + ) + ->toArray(); $traceProperty = new ReflectionProperty('Exception', 'trace'); $traceProperty->setAccessible(true);