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

[10.x] Fix mapped renderable exception handling #47347

Merged
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
4 changes: 3 additions & 1 deletion src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ protected function context()
*/
public function render($request, Throwable $e)
{
$e = $this->mapException($e);

if (method_exists($e, 'render') && $response = $e->render($request)) {
return Router::toResponse($request, $response);
}
Expand All @@ -381,7 +383,7 @@ public function render($request, Throwable $e)
return $e->toResponse($request);
}

$e = $this->prepareException($this->mapException($e));
$e = $this->prepareException($e);

if ($response = $this->renderViaCallbacks($request, $e)) {
return $response;
Expand Down
24 changes: 24 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ public function testReturnsCustomResponseFromCallableClass()
$this->assertSame('{"response":"The CustomRenderer response"}', $response);
}

public function testReturnsResponseFromRenderableException()
{
$response = $this->handler->render(Request::create('/'), new RenderableException)->getContent();

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

public function testReturnsResponseFromMappedRenderableException()
{
$this->handler->map(RuntimeException::class, RenderableException::class);

$response = $this->handler->render(Request::create('/'), new RuntimeException)->getContent();

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

public function testReturnsCustomResponseWhenExceptionImplementsResponsable()
{
$response = $this->handler->render($this->request, new ResponsableException)->getContent();
Expand Down Expand Up @@ -450,6 +466,14 @@ public function report()
}
}

class RenderableException extends Exception
{
public function render($request)
{
return response()->json(['response' => 'My renderable exception response']);
}
}

class ContextProvidingException extends Exception
{
public function context()
Expand Down