diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index bd8016329e6f..38c29a3996c7 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -278,6 +278,10 @@ protected function shouldntReport(Throwable $e) */ protected function exceptionContext(Throwable $e) { + if (method_exists($e, 'context')) { + return $e->context(); + } + return []; } diff --git a/tests/Foundation/FoundationExceptionsHandlerTest.php b/tests/Foundation/FoundationExceptionsHandlerTest.php index f7175dc58db5..55b2d54982a5 100644 --- a/tests/Foundation/FoundationExceptionsHandlerTest.php +++ b/tests/Foundation/FoundationExceptionsHandlerTest.php @@ -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); @@ -283,6 +292,16 @@ public function report() } } +class ContextProvidingException extends Exception +{ + public function context() + { + return [ + 'foo' => 'bar', + ]; + } +} + interface ReportingService { public function send($message);