Skip to content

Commit

Permalink
Pass the request to the renderable callback (#34200)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebdesign authored Sep 8, 2020
1 parent d9b312c commit 4026844
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public function render($request, Throwable $e)

foreach ($this->renderCallbacks as $renderCallback) {
if (is_a($e, $this->firstClosureParameterType($renderCallback))) {
$response = $renderCallback($e);
$response = $renderCallback($e, $request);

if (! is_null($response)) {
return $response;
Expand Down
23 changes: 20 additions & 3 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,26 @@ public function testReturnsJsonWithStackTraceWhenAjaxRequestAndDebugTrue()
$this->assertStringContainsString('"trace":', $response);
}

public function testReturnsCustomResponseWhenExceptionImplementsResponsable()
public function testReturnsCustomResponseFromRenderableCallback()
{
$this->handler->renderable(function (CustomException $e, $request) {
$this->assertSame($this->request, $request);

return response()->json(['response' => 'My custom exception response']);
});

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

$this->assertSame('{"response":"My custom exception response"}', $response);
}

public function testReturnsCustomResponseWhenExceptionImplementsResponsable()
{
$response = $this->handler->render($this->request, new ResponsableException)->getContent();

$this->assertSame('{"response":"My responsable exception response"}', $response);
}

public function testReturnsJsonWithoutStackTraceWhenAjaxRequestAndDebugFalseAndExceptionMessageIsMasked()
{
$this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(false);
Expand Down Expand Up @@ -224,11 +237,15 @@ public function testSuspiciousOperationReturns404WithoutReporting()
}
}

class CustomException extends Exception implements Responsable
class CustomException extends Exception
{
}

class ResponsableException extends Exception implements Responsable
{
public function toResponse($request)
{
return response()->json(['response' => 'My custom exception response']);
return response()->json(['response' => 'My responsable exception response']);
}
}

Expand Down

0 comments on commit 4026844

Please sign in to comment.