Skip to content

Commit

Permalink
Check for context method (#36424)
Browse files Browse the repository at this point in the history
  • Loading branch information
avrahamappel authored Mar 2, 2021
1 parent bdd082d commit bbd6e02
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ protected function shouldntReport(Throwable $e)
*/
protected function exceptionContext(Throwable $e)
{
if (method_exists($e, 'context')) {
return $e->context();
}

return [];
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public function testHandlerReportsExceptionAsContext()
$this->handler->report(new RuntimeException('Exception message'));
}

public function testHandlerCallsContextMethodIfPresent()
{
$logger = m::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);
$logger->shouldReceive('error')->withArgs(['Exception message', m::subset(['foo' => 'bar'])])->once();

$this->handler->report(new ContextProvidingException('Exception message'));
}

public function testHandlerReportsExceptionWhenUnReportable()
{
$logger = m::mock(LoggerInterface::class);
Expand Down Expand Up @@ -283,6 +292,16 @@ public function report()
}
}

class ContextProvidingException extends Exception
{
public function context()
{
return [
'foo' => 'bar',
];
}
}

interface ReportingService
{
public function send($message);
Expand Down

0 comments on commit bbd6e02

Please sign in to comment.