我不知道抛出异常是怎么回事.
当我在查找中输入一个不好的数字时,我会得到以下信息:
致命错误:在/home/jimbursch/includes/twilio-php/Services/Twilio.php:297堆栈跟踪中未找到“未找到所请求的资源/PhoneNumbers/310-69-5340”消息的未命名异常“Services_Twilio_RestException”:#0 /home/jimbursch/includes/twilio-php/Services/Twilio.php(265):Base_Services_Twilio->_processResponse( /home/jimbursch/includes/twilio-php/Services/Twilio.php(236):Base_Services_Twilio->_makeIdempotentRequest(Array,) #1 /home/jimbursch/includes/twilio-php/Services/Twilio.php(236):Base_Services_Twilio->_makeIdempotentRequest(Array,‘/v1/PhoneNumber.’,1) #2 /home/jimbursch/includes/twilio-php/Services/Twilio/InstanceResource.php(79):Base_Services_Twilio->retrieveData('/v1/PhoneNumber...') #3 /home/jimbursch/includes/site_functions.php(655):Services_Twilio_InstanceResource->__get('phone_number') #4 /home/jimbursch/include/admin/misc.php(43):lookupPhone('310-69-5340')
这就是我认为它正在发生的地方:
private function _processResponse($response)
{
list($status, $headers, $body) = $response;
if ($status === 204) {
return true;
}
$decoded = json_decode($body);
if ($decoded === null) {
throw new Services_Twilio_RestException(
$status,
'Could not decode response body as JSON. ' .
'This likely indicates a 500 server error'
);
}
if (200 <= $status && $status < 300) {
$this->last_response = $decoded;
return $decoded;
}
throw new Services_Twilio_RestException(
$status,
isset($decoded->message) ? $decoded->message : '',
isset($decoded->code) ? $decoded->code : null,
isset($decoded->more_info) ? $decoded->more_info : null
);
}发布于 2016-01-27 19:25:34
你必须catch这个例外。
正确的是,当发生错误时,Twilio SDK抛出一个异常。
你必须做如下事情:
<?php
...
try {
//Your Twilio code you'd like to execute
} catch( Services_Twilio_RestException $e ) {
echo $e->getMessage(); // Or maybe log it
// Handle the fact that "The requested resource /PhoneNumbers/310-69-5340 was not found"
}通过捕获错误消息,您可以避免Fatal error,并且您的脚本可以继续工作,从而允许您注册操作失败(或者报告了其他有用的信息)。
https://stackoverflow.com/questions/35042653
复制相似问题