Hi everyone,
I am a bit lost in how to capture a 404 error in an API route.
I have this in the routes\api.php file
Route::middleware('auth:sanctum')->put('/v1/activations/change_status/{activation}', [ActivationController::class, 'change_status']);
which is handled by the following controller action
public function change_status(ChangeActivationStatus $request, Activation $activation)
{
... the rest of the code goes here
}
The thing that when I send a valid activation id as the parameter say for example
[endpoint_base_url]/api/v1/activations/change_status/42
It works as expected but if I send an invalid id say for example
[endpoint_base_url]/api/v1/activations/change_status/43
Then it returns a 404 Not Found error to Postman. My guess is that this is happening because when the controller is trying to find an Activation model for the id = 43 it is coming null and throwing the Not Found exception which is ok but I will like to catch this and generate a customized JSON response error for example.
{
"success": false,
"message": "There is no activation with the provided id."
}
But I am lost on how to catch the event and generate this customized JSON response.