Skip to content
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
5 changes: 2 additions & 3 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
);
}
Expand 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() : [],
Expand Down
44 changes: 44 additions & 0 deletions tests/Integration/Foundation/ExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
]);
}
}
Loading