diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index cdd7b7d16736..57dfc30f480e 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -22,7 +22,6 @@ use Illuminate\Database\RecordsNotFoundException; use Illuminate\Foundation\Exceptions\Renderer\Renderer; use Illuminate\Http\Exceptions\HttpResponseException; -use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Response; use Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException; @@ -985,7 +984,7 @@ protected function toIlluminateResponse($response, Throwable $e) $response->getTargetUrl(), $response->getStatusCode(), $response->headers->all() ); } else { - $response = new Response( + $response = response( $response->getContent(), $response->getStatusCode(), $response->headers->all() ); } @@ -1002,7 +1001,7 @@ protected function toIlluminateResponse($response, Throwable $e) */ protected function prepareJsonResponse($request, Throwable $e) { - return new JsonResponse( + return response()->json( $this->convertExceptionToArray($e), $this->isHttpException($e) ? $e->getStatusCode() : 500, $this->isHttpException($e) ? $e->getHeaders() : [], diff --git a/tests/Integration/Foundation/ExceptionHandlerTest.php b/tests/Integration/Foundation/ExceptionHandlerTest.php index a53cae5341eb..b4a52ecee635 100644 --- a/tests/Integration/Foundation/ExceptionHandlerTest.php +++ b/tests/Integration/Foundation/ExceptionHandlerTest.php @@ -2,11 +2,15 @@ namespace Illuminate\Tests\Integration\Foundation; +use Exception; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\Access\Response; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Debug\ShouldntReport; +use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract; use Illuminate\Contracts\Support\Responsable; +use Illuminate\Http\JsonResponse; +use Illuminate\Routing\ResponseFactory; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Route; use Orchestra\Testbench\TestCase; @@ -237,4 +241,44 @@ public function test_it_handles_malformed_error_views_in_development() $this->assertNotNull($response); $response->assertStatus(500); } + + public function test_it_use_custom_json_response_factory_in_exception_handler() + { + $this->app->singleton(ResponseFactoryContract::class, function ($app) { + return new class($app['view'], $app['redirect']) extends ResponseFactory + { + public function json($data = [], $status = 200, array $headers = [], $options = 0) + { + $msg = $data['message'] ?? $data['msg'] ?? null; + if ($msg) { + unset($data['message']); + $wrapData = [ + 'msg' => $msg, + 'success' => $status >= 200 && $status < 300, + ] + $data; + } else { + $wrapData = [ + 'msg' => 'success', + 'success' => true, + 'data' => $data, + ]; + } + + return new JsonResponse($wrapData, 200, $headers, $options); + } + }; + }); + + Route::get('test-exception', function () { + throw new Exception('Test exception'); + }); + + $response = $this->getJson('test-exception'); + + $response->assertStatus(200); + $response->assertJson([ + 'msg' => 'Server Error', + 'success' => false, + ]); + } }