Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Accept callable class for reportable and renderable in exception handler #36551

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public function register()
*/
public function reportable(callable $reportUsing)
{
if (! $reportUsing instanceof Closure) {
$reportUsing = Closure::fromCallable($reportUsing);
}

return tap(new ReportableHandler($reportUsing), function ($callback) {
$this->reportCallbacks[] = $callback;
});
Expand All @@ -152,6 +156,10 @@ public function reportable(callable $reportUsing)
*/
public function renderable(callable $renderUsing)
{
if (! $renderUsing instanceof Closure) {
$renderUsing = Closure::fromCallable($renderUsing);
}

$this->renderCallbacks[] = $renderUsing;

return $this;
Expand Down
48 changes: 48 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ public function testHandlerCallsReportMethodWithDependencies()
$this->handler->report(new ReportableException('Exception message'));
}

public function testHandlerReportsExceptionUsingCallableClass()
{
$reporter = m::mock(ReportingService::class);
$reporter->shouldReceive('send')->withArgs(['Exception message'])->once();

$logger = m::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);
$logger->shouldNotReceive('error');

$this->handler->reportable(new CustomReporter($reporter));

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

public function testReturnsJsonWithStackTraceWhenAjaxRequestAndDebugTrue()
{
$this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(true);
Expand Down Expand Up @@ -134,6 +148,15 @@ public function testReturnsCustomResponseFromRenderableCallback()
$this->assertSame('{"response":"My custom exception response"}', $response);
}

public function testReturnsCustomResponseFromCallableClass()
{
$this->handler->renderable(new CustomRenderer());

$response = $this->handler->render($this->request, new CustomException)->getContent();

$this->assertSame('{"response":"The CustomRenderer response"}', $response);
}

public function testReturnsCustomResponseWhenExceptionImplementsResponsable()
{
$response = $this->handler->render($this->request, new ResponsableException)->getContent();
Expand Down Expand Up @@ -302,6 +325,31 @@ public function context()
}
}

class CustomReporter
{
private $service;

public function __construct(ReportingService $service)
{
$this->service = $service;
}

public function __invoke(CustomException $e)
{
$this->service->send($e->getMessage());

return false;
}
}

class CustomRenderer
{
public function __invoke(CustomException $e, $request)
{
return response()->json(['response' => 'The CustomRenderer response']);
}
}

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