Im trying to catch this error without try catch block. but it doesn't work . please help me to fix this .
RegisterController.php
$isUserCreate = User::findorFail(30);
throw_if(!$isUserCreate, Exception::class, 'test', 404);
Handler.php
public function register()
{
$this->renderable(function (Exception $e,$request) {
return Response::json([
'message' => $e->getMessage(),
'code' => $e->getCode(),
], $e->getCode());
});
}
Output
POST http://127.0.0.1:8000/api/auth/register
HTTP/1.1 404 Not Found
Host: 127.0.0.1:8000
Date: Fri, 04 Feb 2022 20:24:25 GMT
Connection: close
X-Powered-By: PHP/8.0.13
Cache-Control: no-cache, private
Date: Fri, 04 Feb 2022 20:24:25 GMT
Content-Type: application/json
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 58
Access-Control-Allow-Origin: *
{
"message": "No query results for model [App\Models\User] 30",
"exception": "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",
"file": "D:\Project Repo\Lectures\Backend\PHP REST API\laravelapidemo\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php",
"line": 385,
"trace": [
{
{