I modified render method of Exceptions/Handler.php to return my own response instead of rendering lumen's error page when any errors occur.
Here is my render method updated.
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
// return parent::render($request, $e);
return response()
->json([
'errors' => $e
]);
}
I expected it return my response when NotFoundHttpException happens, but lumen still shows its original error page...
I understand I should modify app/Exceptions/Handler.php
, but because it doesn't work as I expect, I modified vendor/laravel/lumen/app/Exceptions/Handler.php
instead.
Here is the updated file.
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
return response(['test'=> 'test']);
// return parent::render($request, $e);
}
It works as I expected (not showing error page but return json response).
In bootstrap/app.php, it seems correctly set to call App\Exceptions\Handler::class
, not laravel\lumen\app\Exceptions\Handler::class
.
/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
I've already tried reinstalling lumen and my docker environment. And I tried to modify Handler.php just after installing lumen (that is no other modification in app), but it doesn't work.
Anyone knows my changes are not reflected??