Skip to content

Commit 4026844

Browse files
authored
Pass the request to the renderable callback (#34200)
1 parent d9b312c commit 4026844

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/Illuminate/Foundation/Exceptions/Handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public function render($request, Throwable $e)
314314

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

319319
if (! is_null($response)) {
320320
return $response;

tests/Foundation/FoundationExceptionsHandlerTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,26 @@ public function testReturnsJsonWithStackTraceWhenAjaxRequestAndDebugTrue()
111111
$this->assertStringContainsString('"trace":', $response);
112112
}
113113

114-
public function testReturnsCustomResponseWhenExceptionImplementsResponsable()
114+
public function testReturnsCustomResponseFromRenderableCallback()
115115
{
116+
$this->handler->renderable(function (CustomException $e, $request) {
117+
$this->assertSame($this->request, $request);
118+
119+
return response()->json(['response' => 'My custom exception response']);
120+
});
121+
116122
$response = $this->handler->render($this->request, new CustomException)->getContent();
117123

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

127+
public function testReturnsCustomResponseWhenExceptionImplementsResponsable()
128+
{
129+
$response = $this->handler->render($this->request, new ResponsableException)->getContent();
130+
131+
$this->assertSame('{"response":"My responsable exception response"}', $response);
132+
}
133+
121134
public function testReturnsJsonWithoutStackTraceWhenAjaxRequestAndDebugFalseAndExceptionMessageIsMasked()
122135
{
123136
$this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(false);
@@ -224,11 +237,15 @@ public function testSuspiciousOperationReturns404WithoutReporting()
224237
}
225238
}
226239

227-
class CustomException extends Exception implements Responsable
240+
class CustomException extends Exception
241+
{
242+
}
243+
244+
class ResponsableException extends Exception implements Responsable
228245
{
229246
public function toResponse($request)
230247
{
231-
return response()->json(['response' => 'My custom exception response']);
248+
return response()->json(['response' => 'My responsable exception response']);
232249
}
233250
}
234251

0 commit comments

Comments
 (0)