Skip to content

Commit 866eadf

Browse files
Merge pull request #189 from spatie/fix-invalid-view-trace
Fix view traces
2 parents b9d935f + 166ad8c commit 866eadf

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/Views/ViewExceptionMapper.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Spatie\LaravelIgnition\Views;
44

5-
use Exception;
65
use Illuminate\Contracts\View\Engine;
76
use Illuminate\Foundation\Application;
87
use Illuminate\Support\Arr;
@@ -19,7 +18,9 @@
1918
class ViewExceptionMapper
2019
{
2120
protected Engine $compilerEngine;
21+
2222
protected BladeSourceMapCompiler $bladeSourceMapCompiler;
23+
2324
protected array $knownPaths;
2425

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

8182
protected function modifyViewsInTrace(IgnitionViewException $exception): void
8283
{
84+
$viewIndex = null;
85+
8386
$trace = Collection::make($exception->getPrevious()->getTrace())
84-
->map(function ($trace) {
87+
->map(function ($trace, $index) use (&$viewIndex) {
8588
if ($originalPath = $this->findCompiledView(Arr::get($trace, 'file', ''))) {
89+
8690
$trace['file'] = $originalPath;
8791
$trace['line'] = $this->getBladeLineNumber($trace['file'], $trace['line']);
92+
93+
if ($viewIndex === null) {
94+
$viewIndex = $index;
95+
}
8896
}
8997

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

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

0 commit comments

Comments
 (0)