Reputation: 65
I have video rooms (via Twig) being created on my page when the users want to start a video chat between each other. When they leave the room, the room is being deleted after a period of time so when I try to access it, it drops an error ({room_id} does not exist}. Below is the function:
/**
* @Route("/video/join/{room_name}", name="videochat_join")
*
* @param $room_name
*
* @return RedirectResponse|Response
*
* @throws \Twilio\Exceptions\ConfigurationException
* @throws \Twilio\Exceptions\TwilioException
*/
public function joinVideo($room_name)
{
$user = $this->getCurrentUser();
$twilio = new Client(getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'));
$room = $twilio->video->v1->rooms($room_name)->fetch();
$roomSid = $room->sid;
$token = new AccessToken(getenv('TWILIO_ACCOUNT_SID'), getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'), 3600, $user->getEmail());
$videoGrant = new VideoGrant();
$videoGrant->setRoom($room_name);
$token->addGrant($videoGrant);
return $this->render('chat/video_join.html.twig', [
'roomSid' => $roomSid,
'roomName' => $room_name,
'accessToken' => $token->toJWT(),
]);
};
How can I catch if the room is no longer available and forward user to 404_room.html.twig? Because it doesn't redirect to the default 404 template.
The error is:
RestException
Twilio\Exceptions\RestException:
[HTTP 404] Unable to fetch record: The requested resource /Rooms/1_2room808823 was not found
at vendor/twilio/sdk/Twilio/Version.php:85
at Twilio\Version->exception(object(Response), 'Unable to fetch record')
(vendor/twilio/sdk/Twilio/Version.php:109)
at Twilio\Version->fetch('GET', '/Rooms/1_2room808823', array())
(vendor/twilio/sdk/Twilio/Rest/Video/V1/RoomContext.php:58)
at Twilio\Rest\Video\V1\RoomContext->fetch()
(src/Controller/Chat/VideoController.php:93)
at App\Controller\Chat\VideoController->joinVideo('1_2room808823')
(vendor/symfony/http-kernel/HttpKernel.php:149)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/http-kernel/HttpKernel.php:66)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/http-kernel/Kernel.php:188)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/index.php:37)
I have tried to do:
try{
($twilio->video->v1->rooms($room_name)->fetch());
echo "Room exists"; //this one is working fine
} catch ( TwilioException $e ) {
echo 'Caught exception: ', $e->getMessage(), "\n"; //this doesn't
}
...without luck
Upvotes: 1
Views: 477
Reputation: 17398
Add the following use
statements to the top of your Controller
class.
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twilio\Exceptions\RestException;
Then wrap your code in a try
/catch
block. If you catch the expected RestException
, then you can throw a NotFoundHttpException
to force the 404 response. For example:
/**
* @Route("/video/join/{room_name}", name="videochat_join")
*
* @param $room_name
*
* @return RedirectResponse|Response
*
* @throws \Twilio\Exceptions\ConfigurationException
* @throws \Twilio\Exceptions\TwilioException
*/
public function joinVideo($room_name)
{
try {
$user = $this->getCurrentUser();
$twilio = new Client(getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'));
$room = $twilio->video->v1->rooms($room_name)->fetch();
$roomSid = $room->sid;
$token = new AccessToken(getenv('TWILIO_ACCOUNT_SID'), getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'), 3600, $user->getEmail());
$videoGrant = new VideoGrant();
$videoGrant->setRoom($room_name);
$token->addGrant($videoGrant);
return $this->render('chat/video_join.html.twig', [
'roomSid' => $roomSid,
'roomName' => $room_name,
'accessToken' => $token->toJWT(),
]);
}
catch (RestException $exception) {
throw new NotFoundHttpException("'{$room_name}' could not be found");
}
}
Upvotes: 2