Skip to content
Advertisement

Why error handling is not working in Laravel

I have set the debug to true on .env file. Added the exceptions correctly but when i am passing invalid or not exist in my database its showing me 404 error but here I put the custom error handling value. Here is my code. (also I put “Use Expectation;” on top so no need of Expectation)

public function show($id)
    {
        //only one author with id
        try
        {
            $event = Event::with('eventCategory')->findOrFail($id);
            return new EventsResource($event);
            //return one author
        }
        catch(Expectation $e)
        {   
            report($e);
            return response()->json(['status'=> false, 'message'=>'invalid data'],200);

        }
    }

Advertisement

Answer

As @user3532758 is implying, you probably want to be catching the base Exception not Expectation.

Also make sure you are referencing Exception from the root namespace, assuming the code you have shown is in a Controller:

try {
    ...
} catch (Exception $e) {
    ...
}

PHP Manual – Classes – Exception

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement