当用户希望彼此之间进行视频聊天时,我在页面上创建了视频室(通过Twig)。当他们离开房间时,房间会在一段时间后被删除,因此当我尝试访问它时,它会抛出一个错误({room_id}不存在}。下面是该函数:
/**
* @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(),
]);
};
如果该房间不再可用,该如何将用户转发到404_room.html.twig?因为它不会重定向到默认的404模板。
错误是:
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)
我试图做:
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
}
...没有运气
答案 0 :(得分:1)
在您的use
类的顶部添加以下Controller
语句。
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twilio\Exceptions\RestException;
然后将代码包装在try
/ catch
块中。如果捕获到预期的RestException
,则可以抛出NotFoundHttpException
以强制执行404响应。例如:
/**
* @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");
}
}