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] Pass the request to the renderable callback #34200

Merged
merged 1 commit into from
Sep 8, 2020
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
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