Laravel API, how to properly handle errors

后端 未结 6 775
后悔当初
后悔当初 2021-01-30 15:16

Anyone know what is the best way to handle errors in Laravel, there is any rules or something to follow ?

Currently i\'m doing this :

public function st         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 15:48

    In your handler.php This should work for handling 404 Exception.

    public function render($request, Throwable $exception ){
        if ($exception instanceof ModelNotFoundException) {
            return response()->json([
                'error' => 'Data not found'
            ], 404);
        }
        return parent::render($request, $exception);
    }
    

提交回复
热议问题