2016-07-19 6 views
1

Ich befolge ein Tutorial zur Verwendung der Google Reseller API. Ich bin zu dem Abschnitt über die Ermittlung, ob ein Kunde bereits in Google Apps vorhanden ist (Schritt 2), gekommen, bin aber im Umgang mit dem Objekt Google_Service_Exception nicht mehr weitergekommen.Problem mit dem Zugriff auf Eigenschaft in Google_Service_Exception

Wenn ein Kunde nicht existiert, gibt ein Aufruf an die API einen Fehler 404 zurück. Ich verwende die code Eigenschaft des Google_Service_Exception Objekts $e, um festzustellen, ob die Antwort einen 404-Fehler hat. Jedoch wenn ich versuche, den Fehlercode zurück mit $e->code mit:

try { 
// Call to the Google Reseller API 
} catch (Google_Service_Exception $e) { 
    if($e->code == 404){ 
    return false; 
    } 
} 

ich die folgenden PHP-Fehlermeldung erhalten:

Fatal error: Cannot access protected property Google_Service_Exception::$code.

Die Google_Service_Exception Klasse ist wie folgt:

<?php 

require_once 'Google/Exception.php'; 

class Google_Service_Exception extends Google_Exception 
{ 
    /** 
    * Optional list of errors returned in a JSON body of an HTTP error response. 
    */ 
    protected $errors = array(); 

    /** 
    * Override default constructor to add ability to set $errors. 
    * 
    * @param string $message 
    * @param int $code 
    * @param Exception|null $previous 
    * @param [{string, string}] errors List of errors returned in an HTTP 
    * response. Defaults to []. 
    */ 
    public function __construct(
     $message, 
     $code = 0, 
     Exception $previous = null, 
     $errors = array() 
) { 
    if (version_compare(PHP_VERSION, '5.3.0') >= 0) { 
     parent::__construct($message, $code, $previous); 
    } else { 
     parent::__construct($message, $code); 
    } 

    $this->errors = $errors; 
    } 

    /** 
    * An example of the possible errors returned. 
    * 
    * { 
    * "domain": "global", 
    * "reason": "authError", 
    * "message": "Invalid Credentials", 
    * "locationType": "header", 
    * "location": "Authorization", 
    * } 
    * 
    * @return [{string, string}] List of errors return in an HTTP response or []. 
    */ 
    public function getErrors() 
    { 
    return $this->errors; 
    } 
} 

Also ich angenommen, der Fehler hat etwas damit zu tun, dass $errors geschützt ist. Ich stelle mir vor, dass es aus einem bestimmten Grund geschützt ist, also war ich ein bisschen vorsichtig, die Klasse zu wechseln. Jede Hilfe/Zeiger in der Arbeit um diesen Fehler würde sehr geschätzt werden. Dank

Antwort

0

Verwenden Sie einfach die getCode() Methode:

try { 
    // Call to the Google Reseller API 
} catch (Google_Service_Exception $e) { 
    if($e->getCode() == 404){ // <- Change is here 
     return false; 
    } 
} 

Google_Service_Exception erstreckt Google_Exception und Google_Exception erstreckt Exception. Sie können die documentation about Exception hier lesen. Sie werden die getCode Methode sehen.

+0

Fantastisch, vielen Dank. Viel einfacher als ich dachte – dbatten

Verwandte Themen