Skip to content

Commit bbd6e02

Browse files
authored
Check for context method (#36424)
1 parent bdd082d commit bbd6e02

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Illuminate/Foundation/Exceptions/Handler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ protected function shouldntReport(Throwable $e)
278278
*/
279279
protected function exceptionContext(Throwable $e)
280280
{
281+
if (method_exists($e, 'context')) {
282+
return $e->context();
283+
}
284+
281285
return [];
282286
}
283287

tests/Foundation/FoundationExceptionsHandlerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ public function testHandlerReportsExceptionAsContext()
7676
$this->handler->report(new RuntimeException('Exception message'));
7777
}
7878

79+
public function testHandlerCallsContextMethodIfPresent()
80+
{
81+
$logger = m::mock(LoggerInterface::class);
82+
$this->container->instance(LoggerInterface::class, $logger);
83+
$logger->shouldReceive('error')->withArgs(['Exception message', m::subset(['foo' => 'bar'])])->once();
84+
85+
$this->handler->report(new ContextProvidingException('Exception message'));
86+
}
87+
7988
public function testHandlerReportsExceptionWhenUnReportable()
8089
{
8190
$logger = m::mock(LoggerInterface::class);
@@ -283,6 +292,16 @@ public function report()
283292
}
284293
}
285294

295+
class ContextProvidingException extends Exception
296+
{
297+
public function context()
298+
{
299+
return [
300+
'foo' => 'bar',
301+
];
302+
}
303+
}
304+
286305
interface ReportingService
287306
{
288307
public function send($message);

0 commit comments

Comments
 (0)