如何在查找错误的数字时修复twilio-php未捕获的异常?

时间:2016-01-27 16:22:24

标签: twilio twilio-php

我对抛出异常的方式很弱......

在查找中输入错误的数字时,我收到以下内容:

  

致命错误:/home/jimbursch/includes/twilio-php/Services/Twilio.php:297未找到异常'Services_Twilio_RestException'并显示消息'找不到请求的资源/ PhoneNumbers / 310-69-5340'堆栈跟踪:#0 /home/jimbursch/includes/twilio-php/Services/Twilio.php(265):Base_Services_Twilio-> _processResponse(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-&gt ; retrieveData('/ v1 / PhoneNumber ...')#3 /home/jimbursch/includes/site_functions.php(655):Services_Twilio_InstanceResource-> __ get('phone_number')#4 / home / jimbursch / includes / 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
    );
}

1 个答案:

答案 0 :(得分:3)

您必须catch该例外。

正确地说,Twilio SDK在发生错误时会抛出exception

您必须执行以下操作:

<?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,并且您的脚本可以继续工作,允许您注册操作失败(或报告其他有用的内容)。